Python IP池 与 IP代理

HTTP

# -*- coding: UTF-8 -*-
import urllib.request
import random

url = 'http://193.112.100.126/test/testip.php'
iplist = ['139.199.132.34:8080','59.48.237.6:8060']

proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"}')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')

print(html)

HTTPS

# -*- coding: UTF-8 -*-
import urllib.request
import random
import ssl

url = 'https://ip.cn'
iplist = ['58.50.2.190:9999','132.232.222.74:8080']

ssl._create_default_https_context = ssl._create_unverified_context

proxy_support = urllib.request.ProxyHandler({'https':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"}')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')

print(html)

但有时会失败。。。
有时成功。。。
关闭SSL 验证好麻烦,下次再好好解决这个问题吧。

免费 IP 地址

https://www.xicidaili.com/
https://proxy.mimvp.com/
http://www.shenjidaili.com/open/

校验 IP

https://ip.cn/
https://www.whatismyip.com/