Python 使用BeautifulSoup使用CSS ID在Web上刮取标记

Python 使用BeautifulSoup使用CSS ID在Web上刮取标记,python,beautifulsoup,data-analysis,Python,Beautifulsoup,Data Analysis,我正试图在这个网站上搜索id为'2004.advanced'(存在)的标签。这是我试过的三行代码 webpage = requests.get('https://www.basketball-reference.com/players/j/jamesle01.html') soup = BeautifulSoup(webpage.content, 'html.parser') print(soup.find_all( attrs = {'id': 'advanced.2004'})) 提前

我正试图在这个网站上搜索id为'2004.advanced'(存在)的标签。这是我试过的三行代码

webpage = requests.get('https://www.basketball-reference.com/players/j/jamesle01.html')

soup = BeautifulSoup(webpage.content, 'html.parser')

print(soup.find_all( attrs = {'id': 'advanced.2004'}))

提前感谢您的帮助

问题是您试图查找的元素在注释中。要解决此问题,请尝试循环浏览页面上的每个注释,使用
BeautifulSoup
解析其内容并搜索所需元素:

导入请求
从bs4导入BeautifulSoup,评论
url='1〕https://www.basketball-reference.com/players/j/jamesle01.html'
网页=请求。获取(url)
soup=BeautifulSoup(webpage.content,'html.parser')
查找所有(text=lambda el:isinstance(el,comment)):
comment\u html=BeautifulSoup(注释'html.parser')
el=comment\u html.find(id='advanced.2004')
如果el!=无:中断
打印(el)

您如何知道元素在注释中?我不知道什么时候检查过网站。@Dapper我在firefox上使用ctrl+u检查过页面源代码,但你说得对,检查网站时,元素实际上不在注释中。我的猜测是,在加载页面后,使用JavaScript删除注释,但是由于我们使用的是<>代码>请求。获取< /COD> JavaScript从未执行。@ DapperL。如果这个答案解决了您的问题,请考虑。这表明您已经找到了解决方案,并为您和回答者赢得了一定的声誉。你可以阅读更多关于它的内容。