python bs4从按钮类内部的属性提取

python bs4从按钮类内部的属性提取,python,button,beautifulsoup,Python,Button,Beautifulsoup,因此,我尝试使用BeautifulSoup4获取属性的值 replay_url_data = matchdatatr[1].findAll("button",{"class":"replay_button_super"}) 这就是我将所有数据放入对象的方式。 在控制台中键入replay\u url\u数据将返回: <button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePop

因此,我尝试使用BeautifulSoup4获取属性的值

replay_url_data = matchdatatr[1].findAll("button",{"class":"replay_button_super"})
这就是我将所有数据放入对象的方式。 在控制台中键入replay\u url\u数据将返回:

<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>
这将返回[]空

replay_url_data[0].find('data-spectate-platform')
这将返回相同的空结果

replay_url_data[0].find('button',attrs={'class' : 'data-spectate-link'})
这个返回与上面相同的[]空

replay_url_data[0].find('data-spectate-platform')
在谷歌上搜索了3个小时后,到目前为止,没有任何东西能帮到我,我变得绝望了。我还是python和html新手,请原谅我的愚蠢。

要获取属性,请使用。attrs[data spectate link]或直接[data spectate link]

范例

from bs4 import BeautifulSoup as BS

text = '<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>'
soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"class": "replay_button_super"})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)

value = one_button.attrs["data-spectate-link"]
print(value)
from bs4 import BeautifulSoup as BS

text = '''<button>Other button</button>
<button>Other button</button>
<button>Other button</button>
<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>
<button>Other button</button>
<button>Other button</button>'''

soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"data-spectate-link": True})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)
非{class:data spectate link}

范例

from bs4 import BeautifulSoup as BS

text = '<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>'
soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"class": "replay_button_super"})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)

value = one_button.attrs["data-spectate-link"]
print(value)
from bs4 import BeautifulSoup as BS

text = '''<button>Other button</button>
<button>Other button</button>
<button>Other button</button>
<button class="replay_button_super" data-client-version="0.0.2.21" data-rel="spectatePopup" data-spectate-encryptionkey="bPPxpLIDmi0hRfU2U8B9Li1VJfTTx6pZ" data-spectate-endpoint="replays.cosmicradiance.com:80" data-spectate-gameid="4339075348" data-spectate-link="/api/spectate/UEJPNkN4MkIwUkZERUJ0MWUyZ3dDTmxGT25kanlUN2V6YnpuZUQ0bVlyMWRReGNDRXprZ1lQVnRnSkNHMG04Y2hUdVhxQm9abHFsQ2VBaTRaYVFPdnc9PQ==" data-spectate-platform="Modigu1" data-width="640"><i class="fa fa-play"></i>Replay</button>
<button>Other button</button>
<button>Other button</button>'''

soup = BS(text, 'html.parser')

all_buttons = soup.findAll("button", {"data-spectate-link": True})
one_button = all_buttons[0]

value = one_button["data-spectate-link"]
print(value)
这就是你想要的

按钮设置汤内的标签。然后使用['data-spectate-link']可以在标记内设置属性

data spectate link是一个属性。要获取data spectate link的值,需要使用元素['data-spectate-link']

您可以使用findAll或CSS选择器select

或Css选择器

replay_url_data =soup.select("button.replay_button_super[data-spectate-link]")
print(replay_url_data[0]['data-spectate-link'])

findAllbutton,{class:replay_button_super}[0][data client version]@furas returns:TypeError:列表索引必须是整数或片,而不是strfirst我忘记了[0]从列表中获取第一项,但现在我的注释有[0]非常感谢您的帮助。我不知道为什么我找不到任何类似于您提供给我的信息。人们大多按类或id搜索,然后会得到文本或href/src-因此具有其他属性的示例可能非常罕见。我认为,即使在它是很难找到它。只是尝试了你的代码,它也工作!但我已经把第一个答案标记为答案。
replay_url_data =soup.select("button.replay_button_super[data-spectate-link]")
print(replay_url_data[0]['data-spectate-link'])