【问题标题】:How to extract href, alt and imgsrc using beautiful soup python如何使用漂亮的汤 python 提取 href、alt 和 imgsrc
【发布时间】: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&amp;template=beez_20&amp;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&amp;template=beez_20&amp;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'] 返回什么?
  • eachMov['alt'] 正确返回具有电影名称的替代文本:好友 2013 马拉雅拉姆语电影。我不知何故需要了解以下

  • 你想只得到&lt;img width="110"&gt;&lt;p class="read more"&gt;标签吗?

标签: python html python-2.7 beautifulsoup


【解决方案1】:

您可以使用以下方法同时获得&lt;img width="110"&gt;&lt;p class="read more"&gt;

for div in soup.find_all(class_='item'):
    # Will match `<p class="readmore">...</p>` that is direct
    # child of the div.
    p = div.find(class_='readmore', recursive=False)

    # Will print `href` attribute of the first `<a>` element
    # inside `p`.
    print p.a['href']

    # Will match `<img width="110">` that is direct child
    # of the div.
    img = div.find('img', width=110, recursive=False)

    print img['src'], img['alt']

请注意,这是最新的 Beautiful Soup 版本。

【讨论】:

  • 我知道我很天真,但你能告诉我如何从 p、img ResultSet 中获取 href、alt、src 吗?
  • 谢谢。收到错误:AttributeError: 'NoneType' 对象没有属性 'a'
  • @gbzygil,显然漏掉了一个错字。现在应该修好了。
  • 仍然出现同样的错误 :( [我已将整个代码粘贴到此处:pastebin.com/FpV2yteS]
【解决方案2】:

我通常使用PyQuery 进行此类报废,它既干净又容易。您可以直接使用 jQuery 选择器。例如,要查看您的姓名和声誉,我只需要写类似

from pyquery import PyQuery as pq

d = pq(url = 'http://stackoverflow.com/users/1234402/gbzygil')
p=d('#user-displayname')
t=d('#user-panel-reputation div h1 a span')
print p.html()

所以除非你不能从 bsoup 切换,否则我强烈建议切换到 PyQuery 或一些支持 XPath 的库。

【讨论】:

  • PyQuery 不是现有设置的一部分。 BeautifulSoup 是。无权安装新...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2020-02-13
  • 2021-01-23
相关资源
最近更新 更多