Python beautifulsoup4 libary find_all()函数问题

时间:2020-03-20 18:47:15

标签: python beautifulsoup

如何仅取“ data-ds-appid”?

<a href="https://store.steampowered.com/app/730/CounterStrike_Global_Offensive/?snr=1_241_4_action_104" class="tab_item  "  data-ds-appid="730" data-ds-itemkey="App_730"

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以通过将标签视为字典1

来访问标签的属性
from bs4 import BeautifulSoup

data = '''<h1>asdfas</h1>
<p>asd aasdfas dfasd
<a href="https://store.steampowered.com/app/730/CounterStrike_Global_Offensive/?snr=1_241_4_action_104" class="tab_item  "  data-ds-appid="730" data-ds-itemkey="App_730">asdfas</a>
</p>
'''

soup = BeautifulSoup(data, 'html.parser')

idlist = [element['data-ds-appid'] for element in soup.find_all(class_="tab_item")]

print(idlist)