【发布时间】:2016-07-10 00:24:23
【问题描述】:
我正在从遵循相同结构的多个 div 中抓取标题、描述、链接和人名。我正在使用 BeautifulSoup,我能够从第一个 div 中刮掉所有东西。但是,我无法从长长的 div 列表中抓取数据,并以 CSV 或 JSON 等可移植格式获取数据。
如何从长长的 div 列表中抓取每个项目,并将这些信息存储在 JSON 对象中,用于每个 mp3?
div 看起来像这样:
<div class="audioBoxWrap clearBoth">
<h3>Title 1</h3>
<p>Description 1</p>
<div class="info" style="line-height: 1px; height: 1px; font-size: 1px;"></div>
<div class="audioBox" style="display: none;">
stuff
</div>
<div> [ <a href="link1.mp3">Right-click to download</a>] </div>
</div>
<div class="audioBoxWrap clearBoth">
<h3>Title 2</h3>
<p>Description 2</p>
<div class="info" style="line-height: 1px; height: 1px; font-size: 1px;"></div>
<div class="audioBox" style="display: none;">
stuff
</div>
<div> [ <a href="link2.mp3">Right-click to download</a>] </div>
</div>
我已经弄清楚如何从第一个 div 中抓取,但我无法获取每个 div 的信息。例如,我下面的代码只会一遍又一遍地为第一个 div 吐出 h3。
我知道我可以为标题、描述等创建一个 python 列表,但是我如何保持像 JSON 这样的元数据结构,以便 title1、link1 和 description1 保持在一起,以及 title2 的信息。
with open ('soup.html', 'r') as myfile:
html_doc = myfile.read()
soup = BeautifulSoup(html_doc, 'html.parser')
audio_div = soup.find_all('div', {'class':"audioBoxWrap clearBoth"})
print len(audio_div)
#create dictionary for storing scraped data. I don't know how to store the values for each mp3 separately.
for i in audio_div:
print soup.find('h3').text
我希望我的 JSON 看起来像这样:
{
"podcasts":[
{
"title":"title1",
"description":"description1",
"link":"link1"
},
{
"title":"title2",
"description":"description2",
"link":"link2"
}
]
}
【问题讨论】:
标签: python json beautifulsoup