【问题标题】:python Beautiful Soup - cannot find the feed URLpython Beautiful Soup - 找不到提要 URL
【发布时间】:2018-07-27 07:01:42
【问题描述】:

我正在使用 python 的Beautiful Soup 模块来获取任何网站的提要 URL。但该代码不适用于所有网站。例如它适用于http://www.extremetech.com/,但不适用于http://cnn.com/。实际上http://cnn.com/ 重定向到https://edition.cnn.com/。所以我使用了后者,但没有运气。但我通过谷歌搜索发现 CNN 的提要是 here

我的代码如下:

import urllib.parse
import requests
import feedparser
from bs4 import BeautifulSoup as bs4
# from bs4 import BeautifulSoup


def findfeed(site):
    user_agent = {
        'User-agent':
            'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'}
    raw = requests.get(site, headers = user_agent).text
    result = []
    possible_feeds = []
    #html = bs4(raw,"html5lib")
    html = bs4(raw,"html.parser")
    feed_urls = html.findAll("link", rel="alternate")



    for f in feed_urls:
        t = f.get("type",None)
        if t:
            if "rss" in t or "xml" in t:
                href = f.get("href",None)
                if href:
                    possible_feeds.append(href)
    parsed_url = urllib.parse.urlparse(site)
    base = parsed_url.scheme+"://"+parsed_url.hostname
    atags = html.findAll("a")
    for a in atags:
        href = a.get("href",None)
        if href:
            if "xml" in href or "rss" in href or "feed" in href:
                possible_feeds.append(base+href)
    for url in list(set(possible_feeds)):
        f = feedparser.parse(url)
        if len(f.entries) > 0:
            if url not in result:
                result.append(url)

    for result_indiv in result:
                print( result_indiv,end='\n  ')
    #return(result)




# findfeed("http://www.extremetech.com/")
# findfeed("http://www.cnn.com/")
findfeed("https://edition.cnn.com/")

如何使代码适用于所有网站,例如 https://edition.cnn.com/ ?我正在使用 python 3。

编辑1:如果我需要使用除Beautiful Soup 之外的任何模块,我已经准备好了

【问题讨论】:

  • requests 跟随重定向;请求www.cnn.com 会将您重定向到edition.cnn.com;您得到的响应是重定向的最后一部分。获取www 并查看响应.history.url 属性,你会看到你最终得到edition
  • RSS 和 atom 提要 可以通过 link 标签自动发现,但这绝不是给定的。 CNN 主页没有这样的链接,所以你在那儿不走运。
  • 快速谷歌搜索到edition.cnn.com/services/rss,但我不知道 CNN 在哪里链接到该页面。将edition.cnn.com 插入具有发现支持的已建立的 RSS 阅读器 (inoreader.com) 表明它也无法找到提要。
  • Istiaque,您应该阅读一些有关如何制作 Web API 的文档。如果你这样做了,你会意识到虽然有些网站可能遵循一个共同的约定,但没有什么要求他们这样做,所以你不能假设一个网站甚至有 rss 提要,更不用说从他们的主页链接到它们了。

标签: python beautifulsoup rss feed


【解决方案1】:

如何使代码适用于所有网站

你不能。并非每个站点都遵循最佳实践。

它是recommended that the site homepage includes a <link rel="alternate" type="application/rss+xml" ...> or <link rel="alternate" type="application/atom+xml" ...> element,但 CNN 不遵循建议。没有办法解决这个问题。

但我通过谷歌搜索发现 CNN 的提要在这里。

那不是主页,CNN 也没有提供任何发现它的方法。目前没有自动方法来发现哪些网站出现了这个错误。

其实http://cnn.com/重定向到https://edition.cnn.com/

请求会自动为您处理重定向:

>>> response = requests.get('http://cnn.com')
>>> response.url
'https://edition.cnn.com/'
>>> response.history
[<Response [301]>, <Response [301]>, <Response [302]>]

如果我需要使用除 BeautifulSoup 之外的任何模块,我已准备好这样做

这不是模块可以解决的问题。有些网站没有实施自动发现或没有正确实施。

例如,已建立的支持自动发现的 RSS 提要软件(如在线 https://inoreader.com),找不到 CNN 提要,除非您使用您找到的特定 /services/rss URL用谷歌搜索。

【讨论】:

    【解决方案2】:

    看着这个answer。这应该可以完美地工作:

    feeds = html.findAll(type='application/rss+xml') + html.findAll(type='application/atom+xml')
    

    CNN RSS service 上尝试该功能完美。您的主要问题是 edition.cnn.com 没有任何形式的 RSS 痕迹。

    【讨论】:

    • 除了 rss 和 atom 之外,我还需要寻找哪些类型?
    • 我不知道。就我所知,这两个是最广泛使用的:)
    • 我的 python shell 一直在等待 edition.cnn.com/services/rss 作为输入。 edition.cnn.comcnn.com 不工作吗?
    • 如果 edition.cnn.com/services/rss 有 RSS 链接,那么edition.cnn.com 也有,对吧?
    • 不,如果这样做的话,它会更加用户友好。但仅仅因为它们在同一个域上并不意味着它将包含 RSS 提要。
    猜你喜欢
    • 2017-12-07
    • 2013-07-15
    • 2019-11-23
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多