代码之家  ›  专栏  ›  技术社区  ›  Ram Rachum

使用Python检索类似Facebook的链接摘要(标题、摘要、相关图片)

  •  7
  • Ram Rachum  · 技术社区  · 15 年前

    。当你提交一个链接到你的Facebook状态时,他们的系统会退出并检索一个建议的 title , summary 通常一个或多个相关的 image

    我真的很想先从别人的经验中学习,然后再插手。

    为了清楚起见,当给定网页的URL时,我希望能够检索:

    1. The title: Probably just the <title> tag but possibly the <h1>
    2. 这一页的一段摘要。
    3. 。(棘手的部分是过滤掉不相关的图像,如横幅或圆角)

    2 回复  |  直到 12 年前
        1
  •  2
  •   Troy Alford    12 年前

    BeautifulSoup 很适合完成大部分工作。

    基本上,您只需初始化 soup 对象,然后执行以下操作以提取您感兴趣的内容:

    title = soup.findAll('title')
    images = soup.findAll('img')
    

    然后,您可以根据 url 使用 urllib2 .

    。?例如,圆角很小,通常只有1-2种颜色。

    As for the page summary, that may be a bit more difficult, but I've been doing something like this:

    1. 我用 BeautifulSoup html 通过使用: .findAll ,然后 .extract .
    2. 我使用以下方法获取剩余文本: .join(soup.findAll(text = True))

    In your application, perhaps you could use this "text" 内容作为页面摘要?

    我希望这能有帮助。

        2
  •  1
  •   ducu    11 年前

    下面是一个完整的解决方案: https://github.com/svven/summary

    >>> import summary
    >>> s = summary.Summary('http://stackoverflow.com/users/76701/ram-rachum')
    >>> s.extract()
    >>> s.title
    u'User Ram Rachum - Stack Overflow'
    >>> s.description
    u'Israeli Python hacker.'
    >>> s.image
    https://www.gravatar.com/avatar/d24c45635a5171615a7cdb936f36daad?s=128&d=identic
    on&r=PG
    >>>