Python uses a proxy to access the server there are 3 main steps:
1. Create a proxy processor Proxyhandler:
Proxy_support = Urllib.request.ProxyHandler (), Proxyhandler is a class whose argument is a dictionary: {' type ': ' Proxy IP: port number '}
What is handler? Handler is also known as a processor, and each handlers knows how to open URLs through a specific protocol, or how to handle various aspects of URL opening, such as HTTP redirection or HTTP cookies.
2. Customize and create a opener:
Opener = Urllib.request.build_opener (Proxy_support)
What is opener? When Python opens a URL link, it uses opener. In fact, the Urllib.request.urlopen () function is actually using the default opener, but here we need to customize a opener to specify handler.
3a. Installing opener
Urllib.request.install_opener (opener)
Install_opener is used to create a (global) default opener, which means that calling Urlopen will use the opener you installed.
3b. Call opener
Opener.open (URL)
This method can be used to obtain URLs as directly as the Urlopen function: it is not usually necessary to call Install_opener, except for convenience.
>>> Proxy_support = Urllib.request.ProxyHandler ({'http':'115.32.41.100:80'})>>>Proxy_support<urllib.request.proxyhandler object at 0x0000000002ee74a8>>>> opener =Urllib.request.build_opener (Proxy_support)>>>opener<urllib.request.openerdirector Object at 0x0000000002f972b0>>>>opener.handlers[<urllib.request.proxyhandler object at 0x0000000002ee74a8>, <urllib.request.unknownhandler object at 0x0000000003197b38>, <urllib.request.httphandler object at 0x0000000003197c18>, < Urllib.request.HTTPDefaultErrorHandler object at 0x0000000003197cc0>, <urllib.request.httpredirecthandler Object at 0x0000000003197ba8>, <urllib.request.ftphandler object at 0x0000000003197dd8>, < Urllib.request.FileHandler object at 0x0000000003197e80>, <urllib.request.httpshandler object at 0x0000000003197e48>, <urllib.request.httperrorprocessor object at 0x0000000003197e10>]>>>opener.addheaders[('user-agent','python-urllib/3.3')]>>> opener.addheaders = [('user-agent','Test_proxy_python3.5_maminyao')]>>>opener.addheaders[('user-agent','Test_proxy_python3.5_maminyao')]>>>
Example of using a random IP to access a URL from the proxy IP list
Importurllib.requestImportRandomurl='http://www.whatismyip.com.tw'IPList= ['115.32.41.100:80','58.30.231.36:80','123.56.90.175:3128']proxy_support= Urllib.request.ProxyHandler ({'http': Random.choice (IPList)}) Opener=Urllib.request.build_opener (proxy_support) opener.addheaders= [('user-agent','Test_proxy_python3.5_maminyao')]urllib.request.install_opener (opener) Response=urllib.request.urlopen (URL) HTML= Response.read (). Decode ('Utf-8')Print(HTML)
Python uses a proxy to access the server