import reimport urllibimport urllib.request as urllib2from http.cookiejar import MozillaCookieJarfrom urllib import parseimport sslssl._create_default_https_context = ssl._create_unverified_contextproxy = {http: :password@ip:port, # 访问http需要https: :password@ip:port, # 访问https需要}proxy_handler = urllib2.ProxyHandler(proxy)opener = urllib2.build_opener(proxy_handler)headers = {User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36,}url_test = ""request = urllib2.Request(url=url_test4, headers=headers)res = opener.open(request)
环境: python 3.8
刚开始的时候,代理只写了proxy = {http: http://user:password@ip:port} ,很多https协议的url都访问不了,要加 https: https://user:password@ip:port 才行。
是不是大家很少用到代理?算是一个小坑