Handling rss redirects with Python/urllib2

前端 未结 2 1830
猫巷女王i
猫巷女王i 2020-12-09 05:21

Calling urrlib2.urlopen on a link to an article fetched from an RSS feed leads to the following error:

urllib2.HTTPError: HTTP Error 301:

2条回答
  •  春和景丽
    2020-12-09 05:34

    Turns out you need to enable Cookies. The page redirects to itself after setting a cookie first. Because urllib2 does not handle cookies by default you have to do it yourself.

    import urllib2
    import urllib
    from cookielib import CookieJar
    
    cj = CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    p = opener.open("http://feeds.nytimes.com/click.phdo?i=8cd5af579b320b0bfd695ddcc344d96c")
    
    print p.read()
    

提交回复
热议问题