无法在.select()方法中使用多个属性

2024-04-19 20:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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"))

Tags: thefromimportgetlinkresitemrequests
1条回答
网友
1楼 · 发布于 2024-04-19 20:29:41

实际上,你的代码运行得很好。我得到你期望的结果了。我只是需要pip安装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

另外,您需要使用最新的lxmlbs4版本。所以你可能需要执行

  • pip3 install lxml upgrade
  • pip3 install beautifulsoup4 upgrade

相关问题 更多 >