我有以下代码:
<div class="offer-wrapper">
<table width="100%" cellspacing="0" cellpadding="0" class="fixed breakword ad_ideYpjG" summary="Anunt" data-id="221265704"
我想使用 Python 和 BS抓取data-id值。
我有以下代码:
<div class="offer-wrapper">
<table width="100%" cellspacing="0" cellpadding="0" class="fixed breakword ad_ideYpjG" summary="Anunt" data-id="221265704"
我想使用 Python 和 BS抓取data-id值。
我已将您的数据作为 html 从中您可以找到主 div 并使用get方法从该 get table 标记中提取该标记的必需属性
html="""<div class="offer-wrapper">
<table width="100%" cellspacing="0" cellpadding="0" class="fixed breakword ad_ideYpjG" summary="Anunt" data-id="221265704">"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"html.parser")
main_div=soup.find("div",class_="offer-wrapper")
print(main_div.find("table").get("data-id"))
输出:
'221265704'