从属于该帖子的网站获取第一张图片

Ahm*_*ani 5 python beautifulsoup web-scraping python-2.7

我编写了一个从博客或任何页面获取所需信息的程序。接下来,我想要实现的是从该页面检索属于相应帖子的第一张图片(就像 Facebook 在分享帖子时所做的那样)。

我能够通过获取带有alt标签的第一张图片在一定程度上实现这一点(因为许多网站的徽标和图标等中没有 alt 标签,所以第一个图片应该属于帖子)。但这在某些情况下似乎不起作用。有没有其他(更好的)方法来实现这一目标?我正在使用 python 2.7.9 和 BeautifulSoup 4。

d = feedparser.parse('http://rss.cnn.com/rss/edition.rss')

for entry in d.entries:
    try:
        if entry.title is not None:
            print entry.title
            print ""
    except Exception, e:
        print e

    try:
        if entry.link is not None:
            print entry.link
            print ""
    except Exception, e:
        print e

    try:
        if entry.published[5:16] is not None:
            print entry.published[5:16]
            print ""
    except Exception, e:
        print e

    try:
        if  entry.category is not None:
            print entry.category
            print ""
    except Exception, e:
        print e

    try:
        if entry.get('summary', '') is not None:
            print entry.get('summary', '')
            print ""
    except Exception, e:
        print e

    time.sleep(5)

    r = requests.get(entry.link, headers = {'User-Agent' : 'Safari/534.55.3 '})
    soup = BeautifulSoup(r.text, 'html.parser') 

    for img in soup.findAll('img'):
        if img.has_attr('alt'):
            if img['src'].endswith('.jpg') == True or img['src'].endswith('.png') == True:
                print img['src']
                break
Run Code Online (Sandbox Code Playgroud)

Rom*_*usi 1

看一下 opengraph 模块可能更实用:

https://pypi.python.org/pypi/opengraph/0.5

并按照您喜欢的方式纠正它。

它将从 HTML 代码中获取“第一张图像”或使用 og:image。

如果你想学习,也可以通过看源码来实现。该模块也使用 BeautifulSoup。

我需要以下猴子补丁来激活抓取作为后备:

import re
from bs4 import BeautifulSoup
from opengraph import OpenGraph

def parser(self, html):
    """
    """
    if not isinstance(html,BeautifulSoup):
        doc = BeautifulSoup(html, from_encoding='utf-8')
    else:
        doc = html
    ogs = doc.html.head.findAll(property=re.compile(r'^og'))
    for og in ogs:
        self[og[u'property'][3:]]=og[u'content']

    # Couldn't fetch all attrs from og tags, try scraping body
    if not self.is_valid() and self.scrape:
        for attr in self.required_attrs:
            if not hasattr(self, attr):
                try:
                    self[attr] = getattr(self, 'scrape_%s' % attr)(doc)
                except AttributeError:
                    pass


OpenGraph.parser = parser
OpenGraph.scrape = True   # workaround for some subtle bug in opengraph
Run Code Online (Sandbox Code Playgroud)

您可能需要处理图像源中的相关 URL,但使用 urlparse 中的 urljoin 则非常简单

import opengraph
...
page = opengraph.OpenGraph(url=link, scrape=True)
...
if page.is_valid():
    ...
    image_url = page.get('image', None)
    ...
    if not image_url.startswith('http'):
        image_url = urljoin(page['_url'], page['image'])
Run Code Online (Sandbox Code Playgroud)

(为了简洁起见,代码片段中省略了一些检查)