如何仅取“ 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"
谢谢!
答案 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)