【发布时间】:2013-12-02 18:08:47
【问题描述】:
有人可以帮我使用漂亮的汤 python 从下面的示例 html 中提取一些数据吗? 这些是我要提取的内容:
href html 链接:示例
/movies/watch-malayalam-movies-online/6106-watch-buddy.html
具有电影名称的替代文本:
好友 2013 马拉雅拉姆语电影
缩略图:例如http://i44.tinypic.com/2lo14b8.jpg
(这些有多次出现..)
可在以下网址获取完整源代码:http:\\olangal.com
示例 html:
<div class="item column-1">
<h2>
<a href="/movies/watch-malayalam-movies-online/6106-watch-buddy.html">
Buddy
</a>
</h2>
<ul class="actions">
<li class="email-icon">
<a href="/component/mailto/?tmpl=component&template=beez_20&link=36bbe22fb7c54b5465609b8a2c60d8c8a1841581" title="Email" onclick="window.open(this.href,'win2','width=400,height=350,menubar=yes,resizable=yes'); return false;">
<img src="/media/system/images/emailButton.png" alt="Email" />
</a>
</li>
</ul>
<img width="110" height="105" alt=" Buddy 2013 Malayalam Movie" src="http://i44.tinypic.com/2lo14b8.jpg" border="0" />
<p class="readmore">
<a href="/movies/watch-malayalam-movies-online/6106-watch-buddy.html">
Read more...
</a>
</p>
<div class="item-separator">
</div>
</div>
<div class="item column-2">
<h2>
<a href="/movies/watch-malayalam-movies-online/6105-watch-pigman.html">
Pigman
</a>
</h2>
<ul class="actions">
<li class="email-icon">
<a href="/component/mailto/?tmpl=component&template=beez_20&link=2b0dfb09b41b8e6fabfd7ed2a035f4d728bedb1a" title="Email" onclick="window.open(this.href,'win2','width=400,height=350,menubar=yes,resizable=yes'); return false;">
<img src="/media/system/images/emailButton.png" alt="Email" />
</a>
</li>
</ul>
<img width="110" height="105" alt="Pigman 2013 Malayalam Movie" src="http://i41.tinypic.com/jpa3ko.jpg" border="0" />
<p class="readmore">
<a href="/movies/watch-malayalam-movies-online/6105-watch-pigman.html">
Read more...
</a>
</p>
<div class="item-separator">
</div>
</div>
更新:终于在@kroolik 的帮助下破解了它。谢谢你。
这对我有用:
for eachItem in soup.findAll("div", { "class":"item" }):
eachItem.ul.decompose()
imglinks = eachItem.find_all('img')
for imglink in imglinks:
imgfullLink = imglink.get('src').strip()
links = eachItem.find_all('a')
for link in links:
names = link.contents[0].strip()
fullLink = "http://olangal.com"+link.get('href').strip()
print "Extracted : " + names + " , " + imgfullLink+" , "+fullLink
【问题讨论】:
-
你之前尝试过什么? BS4 的文档中清楚地描述了属性提取。
-
这是我用 1 天的 Python 知识和美丽的汤所管理的:
for eachMov in soup.findAll('img', width="110"): print eachMov['alt'].strip() +':'+ eachMov['src'].strip() print eachMov.name不知道如何获取 ALT 文本以及这些 -
eachMov['alt']返回什么? -
你想只得到
<img width="110">和<p class="read more">标签吗?
标签: python html python-2.7 beautifulsoup