【问题标题】:Find by style (color) with requests_html使用 requests_html 按样式(颜色)查找
【发布时间】:2022-01-05 08:54:14
【问题描述】:

对于 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 python web-scraping find python-requests-html


    【解决方案1】:

    我确实将 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)
    

    【讨论】:

    • 非常感谢,有了 Selenium,我明白了 :)
    【解决方案2】:

    编辑:在这种情况下,样式是在页面加载后由 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])
    

    你可以找到所有的&lt;a&gt;标签,如果它们有那个属性的话,把它们放在一个列表中。

    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。它不仅功能更强大,而且用户群更大,因此更容易为您的问题找到解决方案。

    【讨论】:

    • 我不知道为什么,但是anchors = r.html.find('a') 没有&lt;a href="/account/0x58e0ff2eb3addd3ce75cc3fbdac3ac3f4e21fa/38-G1x" style="color:red"&gt;38-G1&lt;/a&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2018-05-31
    • 2012-09-14
    • 1970-01-01
    相关资源
    最近更新 更多