首页 文章

Python / Feedparser:阅读RSS提要失败

提问于
浏览
0

我正在使用feedparser来获取RSS提要数据 . 对于大多数可以正常工作的RSS源 . 但是,我知道偶然发现一个提取RSS源的网站失败了(example feed) . 返回结果不包含预期的键,值是一些HTML代码 .

我尝试使用 urllib2.Request(url) 简单地阅读供稿网址 . 这失败并出现 HTTP Error 405: Not Allowed 错误 . 如果我添加一个自定义 Headers

headers = {
    'Content-type' : 'text/xml',
    'User-Agent': 'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0',
}

request = urllib2.Request(url)

我不再得到405错误,但返回的内容是一个HTML文档,其中包含一些HEAD标记和一个基本上为空的BODY . 在浏览器中,当我看到"View Page Source"时,一切看起来都很好 . feedparser.parse 也允许设置 agentrequest_headers ,我试过各种代理 . 我仍然无法正确读取XML,更不用说来自 feedparse 的解析后的Feed了 .

我在这里错过了什么?

1 回答

  • 0

    因此,当发出请求的客户端不使用 User-Agent 时,此Feed会产生 405 错误 . 试试这个:

    $ curl 'http://www.propertyguru.com.sg/rss' -H 'User-Agent: hum' -o /dev/null -D- -s
    HTTP/1.1 200 OK
    Server: nginx
    Date: Thu, 21 May 2015 15:48:44 GMT
    Content-Type: application/xml; charset=utf-8
    Content-Length: 24616
    Connection: keep-alive
    Vary: Accept-Encoding
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Pragma: no-cache
    Vary: Accept-Encoding
    

    没有UA,你会得到:

    $ curl 'http://www.propertyguru.com.sg/rss' -o /dev/null -D- -s
    HTTP/1.1 405 Not Allowed
    Server: nginx
    Date: Thu, 21 May 2015 15:49:20 GMT
    Content-Type: text/html
    Transfer-Encoding: chunked
    Connection: keep-alive
    Vary: Accept-Encoding
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Pragma: no-cache
    Vary: Accept-Encoding
    

相关问题