Python:Beautiful Soup解析</li>和<ul>并更改类名

2024-06-09 05:50:21 发布

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

我拿不到绿色数据(

displays the HTML structure

yo = requests.get('http://www.nfl.com/schedules/2019/REG11')
soup = bs.BeautifulSoup(yo.text, 'html.parser')
table = soup.find('ul', class_="schedules-table")
print(table) #correctly gathers all data and extraneous data 

Tags: 数据comhttpdatagetbswwwtable
2条回答

绿色突出显示的内容是评论。您可以使用以下代码获取它们:

from bs4 import Comment
comments = table.find_all(string=lambda text: isinstance(text, Comment))

它会把他们都列在一张单子上。你可以把它们编码成一个数据帧或者其他你想要的东西

您看到的绿色数据是html中的注释。可以使用bs4中的Comment类获取它-

from bs4 import BeautifulSoup
from bs4 import Comment
yo = requests.get('http://www.nfl.com/schedules/2019/REG11')
soup = BeautifulSoup(yo.text, 'html.parser')
comment = soup.find_all(string=lambda text: isinstance(text, Comment))

会给你所有的评论。你必须自己过滤掉相关的评论

相关问题 更多 >