Search code examples
pythonproxyurllib2

urllib2 allow redirects on a url using proxy


I am using urllib2 to request a url using proxy. However response is HttpError 302 Found saying redirection is not allowed.

Code:

import urllib2
opener = urllib2.build_opener(proxy_handler)
opener.addheaders = [('User-Agent', user_agent)]
urllib2.install_opener(opener)
print('Performing request')
return urllib2.urlopen(url)

Detailed Error:

HTTPError: HTTP Error 302: Found - Redirection to url 'market://details/?id=com.UCMobile.intl&referrer=utm_source%3Dhexn%40adinallBrowser%25239%26utm_content%3Dclickid%253A29lu41t4865766358092300dfe678d89%253Buc_trans_1%253AhiKpNb0003n190X0FH00CHdX4qZ1hm%253Bct%253A201805291855%253Bhn%253A3ddee1' is not allowed

Solution

  • I think your answer is here: Proxy with urllib2

    You use proxy_handler but don't seem to declare it.

    proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
    opener = urllib2.build_opener(proxy)