Preventing a “hidden” redirect with urlopen() in Python

前端 未结 1 1291
北海茫月
北海茫月 2021-01-03 09:47

I am using BeautifulSoup for web scraping and I am having problems with a particular type of website when using urlopen. Every item on the

相关标签:
1条回答
  • 2021-01-03 10:43
    import urllib2
    
    class RedirectHandler(urllib2.HTTPRedirectHandler):
        def http_error_302(self, req, fp, code, msg, headers):
            result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
            result.status = code
            return result
        http_error_301 = http_error_303 = http_error_307 = http_error_302
    
    opener = urllib2.build_opener(RedirectHandler())
    webpage = opener.open('http://www.example.com/product1/456')
    ...
    
    0 讨论(0)
提交回复
热议问题