Python 3.x 如何通过CSS选择器的字符串选择此元素?

Python 3.x 如何通过CSS选择器的字符串选择此元素?,python-3.x,beautifulsoup,css-selectors,Python 3.x,Beautifulsoup,Css Selectors,从这里,我试图提取链接https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimerin <a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table&

从这里,我试图提取链接
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>

你能详细说明一下结果是怎样的吗?

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

<title>Access denied | www.collinsdictionary.com used Cloudflare to restrict access</title>
这将为您提供以下结果:

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

非常感谢您!我认为
user\u agent
没有那么重要:(.是否可以修改
result=soup.select('div.content.definitions.dictionary.biling>div.hom>span>span.xr>a')
以获取
href=
之后的值?通过获取href值,您必须使用:
link=result[0]。attrs[“href]
。这将获得href值。
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>]