【发布时间】:2019-11-13 13:22:21
【问题描述】:
我想打印出具有唯一类的页面中的所有 ID。
我想用Beautiful Soup 刮的页面是这样的:
<div itemscope itemprop="item" itemtype="http://schema.org/Product" id="12345" class="realestate">
<div class="contentArea">
<meta itemprop="name" content="Name - 12345 " />
<meta itemprop="url" content="https://url12345.hu" />
<meta itemprop="category" content="category1" />
</div>
</div>
<div itemscope itemprop="item" itemtype="http://schema.org/Product" id="12346" class="realestate">
<div class="contentArea">
<meta itemprop="name" content="Name - 12346 " />
<meta itemprop="url" content="https://url12346.hu" />
<meta itemprop="category" content="category1" />
</div>
</div>
“ID”是 Itemscope DIV 中的唯一标识符,因此我想以某种方式提取这些唯一 ID 并将它们全部打印出来(原因是将所有其他广告信息附加到此 ID(如名称、URL、等)稍后)
我尝试使用此 python 代码,但它不起作用。
import requests
from bs4 import BeautifulSoup
page = requests.get('searchResultPage.url')
soup = BeautifulSoup(page.text, 'html.parser')
id = soup.find_all('id')
print(id)
它返回一个空列表。
我所期望的,我想要的是从 div 中取回一个带有 ID-s 的列表,这样: 12345 12346
提前感谢您的帮助!
【问题讨论】:
标签: python beautifulsoup