【问题标题】:How to select this element by its string of CSS selectors?如何通过其 CSS 选择器字符串选择此元素?
【发布时间】:2020-11-19 05:06:45
【问题描述】:

从这个url,我正在尝试提取链接https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer in

<a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>

其 CSS 选择器为 div.content.definitions.dictionary.biling &gt; div.hom &gt; span &gt; span.xr &gt; a。我按照 Automate the Boring Stuff with Python 一书中的说明操作

from bs4 import BeautifulSoup

url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
soup = BeautifulSoup(url, 'html.parser')

soup.select('div.content.definitions.dictionary.biling > div.hom > span > span.xr > a')

能否请您详细说明[]的结果如何?

【问题讨论】:

    标签: python-3.x beautifulsoup css-selectors


    【解决方案1】:

    这是因为柯林斯词典使用 Cloudfare 来提高其网站和服务的安全性和性能。因此,当您向其服务器请求时。它不会给你 HTML 文件。

    <title>Access denied | www.collinsdictionary.com used Cloudflare to restrict access</title>
    

    为了通过它的安全性。您必须在请求中设置用户代理。

    from bs4 import BeautifulSoup
    import requests
    
    user_agent = {'User-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"}
    
    url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
    doc = requests.get(url, headers = user_agent).text
    soup = BeautifulSoup(doc, 'html.parser')
    result = soup.select('div.content.definitions.dictionary.biling > div.hom > span > span.xr > a')
    print(result)
    

    这将为您提供结果:

    [<a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>]
    

    【讨论】:

    • 非常感谢!我认为user_agent不是那么重要:(。是否可以修改result = soup.select('div.content.definitions.dictionary.biling &gt; div.hom &gt; span &gt; span.xr &gt; a')以获得href=之后的值?
    • 通过获取你必须使用的 href 值:link = result[0].attrs["href"]。这将获得 href 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多