按样式(颜色)查找 requests_html
Find by style (color) with requests_html
我必须对 JavaScript 内容使用 requests_html
。代码:
<td class="text-left worker-col truncated"><a href="/account/0x58e0ff2eb3addd3ce75cc3fbdac3ac3f4e21fa/38-G1x" style="color:red">38-G1</a></td>
我想找到所有红色的名称(在本例中为 38-G1)。我想按 style="color:red"
搜索它们。 requests_html
这可能吗?我该怎么做?
编辑:在这种情况下,样式是在页面加载后由 javascript 添加的,因此您必须等待整个页面加载完毕才能抓取它,因此 Selenium 是最佳选择。
您可以像 Fazlul 那样抓取页面:
from bs4 import BeautifulSoup as bs
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver.get("URL")
time.sleep(5)
html = bs(driver.page_source, 'html.parser')
那么你可以使用 CSS 通配符选择器,然后打印出它们的 innerText:
anchors = html.select('a[style*="color:red"]')
print([a.text for a in anchors])
或
您可以找到所有 <a>
标签并将它们放入列表中(如果它们具有该属性)。
anchors = html.select('a')
names = []
for a in anchors:
if 'style' in a.attrs and "color:red" in a.attrs['style']:
names.append(a.text)
编辑:我看到其他用户用 BeautifulSoup 给了你一个解决方案,我想补充一点,如果你是网络抓取的新手,但你打算学习更多,我也建议学习使用 BeautifulSoup。它不仅更强大,而且它的用户群也更大,因此更容易找到解决问题的方法。
我确实将 html session 和 selenium 与 bs4 一起使用。 Selenium 工作正常,但 html 会话无法呈现 js。
用 selenium 编码。(成功)
from bs4 import BeautifulSoup
import time
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
url = URL
driver.get(url)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'html.parser')
for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'):
txt= t.select_one('td:nth-child(2) > a')
text= txt.text if txt else None
print(text)
输出:
38-G15
47_G15_2
47-G1
49-O15
90_GGX
91_ASF
105_MGPM_3
112-GG3
121-APRO
188-MGPM1
198-AP
248_MGPM_1
262-GUD
265_ASF
302-AD
355-GUD.2
Rig_3471855
rigEdge
107_MGPM_3
None
None
使用 html 会话编码(不渲染 js)
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
response = session.get(URL)
soup = BeautifulSoup(response.content, 'html.parser')
for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'):
txt= t.select_one('td:nth-child(2) > a')
text= txt.text if txt else None
print(text)
我必须对 JavaScript 内容使用 requests_html
。代码:
<td class="text-left worker-col truncated"><a href="/account/0x58e0ff2eb3addd3ce75cc3fbdac3ac3f4e21fa/38-G1x" style="color:red">38-G1</a></td>
我想找到所有红色的名称(在本例中为 38-G1)。我想按 style="color:red"
搜索它们。 requests_html
这可能吗?我该怎么做?
编辑:在这种情况下,样式是在页面加载后由 javascript 添加的,因此您必须等待整个页面加载完毕才能抓取它,因此 Selenium 是最佳选择。
您可以像 Fazlul 那样抓取页面:
from bs4 import BeautifulSoup as bs
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver.get("URL")
time.sleep(5)
html = bs(driver.page_source, 'html.parser')
那么你可以使用 CSS 通配符选择器,然后打印出它们的 innerText:
anchors = html.select('a[style*="color:red"]')
print([a.text for a in anchors])
或
您可以找到所有 <a>
标签并将它们放入列表中(如果它们具有该属性)。
anchors = html.select('a')
names = []
for a in anchors:
if 'style' in a.attrs and "color:red" in a.attrs['style']:
names.append(a.text)
编辑:我看到其他用户用 BeautifulSoup 给了你一个解决方案,我想补充一点,如果你是网络抓取的新手,但你打算学习更多,我也建议学习使用 BeautifulSoup。它不仅更强大,而且它的用户群也更大,因此更容易找到解决问题的方法。
我确实将 html session 和 selenium 与 bs4 一起使用。 Selenium 工作正常,但 html 会话无法呈现 js。
用 selenium 编码。(成功)
from bs4 import BeautifulSoup
import time
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
url = URL
driver.get(url)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'html.parser')
for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'):
txt= t.select_one('td:nth-child(2) > a')
text= txt.text if txt else None
print(text)
输出:
38-G15
47_G15_2
47-G1
49-O15
90_GGX
91_ASF
105_MGPM_3
112-GG3
121-APRO
188-MGPM1
198-AP
248_MGPM_1
262-GUD
265_ASF
302-AD
355-GUD.2
Rig_3471855
rigEdge
107_MGPM_3
None
None
使用 html 会话编码(不渲染 js)
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
response = session.get(URL)
soup = BeautifulSoup(response.content, 'html.parser')
for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'):
txt= t.select_one('td:nth-child(2) > a')
text= txt.text if txt else None
print(text)