如何在html中抓取非文本?

2024-09-24 22:33:18 发布

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

我试图从循环中的每个容器中获取唯一的信息。我正在使用python3.7和BeautifulSoup来刮

我遇到了一个问题,我试图得到唯一的球员身份证号码

以下是编号嵌套的标记:

<a cache="true" class="flexpop" content="tabs#ppc" fpopheight="357px" fpopwidth="490px" href="" instance="_ppc" leagueid="216415" playerid="14880" seasonid="2018" tab="null" teamid="-2147483648"> /a>

我尝试了a.split()将a标记转换成一个列表,在那里我可以只指示我想要的数据,但这不起作用

我试着使用选择功能;a、 选择(“playerid”),但是得到像这样的空括号[]

非常感谢您的帮助!谢谢


Tags: 标记信息truecachecontent容器编号class
2条回答

您也可以使用以下语法

from bs4 import BeautifulSoup as bs
h = '<a cache="true" class="flexpop" content="tabs#ppc" fpopheight="357px" fpopwidth="490px" href="" instance="_ppc" leagueid="216415" playerid="14880" seasonid="2018" tab="null" teamid="-2147483648"> /a>'
soup = bs(h,'lxml')
print(soup.select_one('a[playerid]')['playerid'])

感谢用户23332,为我指明了找到答案的方向

我只需要做:

a.attrs['playerid']

相关问题 更多 >