我用 python 编写了一个脚本,用于从网站获取不同帖子的不同链接。如果我选择任何单个属性,例如 a[href^='/questions/']
或 .question-hyperlink
,我可以获得所需的输出。
My question is: how can I use two attributes from the same node within the
.select()
method like I've tried below?
到目前为止我的尝试(没有结果;也没有错误):
import requests
from bs4 import BeautifulSoup
link = "https://stackoverflow.com/questions/tagged/web-scraping"
res = requests.get(link)
soup = BeautifulSoup(res.text,'lxml')
for item in soup.select("a[href^='/questions/'].question-hyperlink"):
print(item.get("href"))
最佳答案
实际上,您的代码运行良好。我得到了你预期的结果。我只需 pip install lxml 和 BeautifulSoup4 即可运行它。
/questions/54035304/unable-to-use-multiple-attributes-within-select-method
/questions/54035268/div-returns-empty-while-scraping-price-using-beautifulsoup-everything-else-lik
/questions/54035199/scrapy-restart-scrapoxy-instances
/questions/54034484/how-i-can-read-multiple-web-addresses-with-sign-in-address-that-block-dynami
附注您需要使用最新的 lxml
和 bs4
版本。所以你可能需要执行
pip3 install lxml --upgrade
pip3 install beautifulsoup4 --upgrade
关于python - 无法在 .select() 方法中使用多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54035304/