我用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安装lxml和BeautifulSoup4来运行它。你知道吗
另外,您需要使用最新的
lxml
和bs4
版本。所以你可能需要执行pip3 install lxml upgrade
pip3 install beautifulsoup4 upgrade
相关问题 更多 >
编程相关推荐