使用代理IP

代理IP可以自己去找,也可以去购买,这有一个免费代理ip网址:http://www.66ip.cn/

下面是代码:


ip = '220.160.23.136' # 代理IP
port = '8888'         # 端口号


##################  一,使用urllib。 #################

import urllib

proxy_handler = urllib.request.ProxyHandler({'http': 'http://%s:%s'%(ip,port)})
request = urllib.request.build_opener(proxy_handler)
r = request.open('http://example.com')
print(r.read())

proxy_handler = urllib.request.ProxyHandler({'http': 'http://%s:%s'%(ip,port)})
request = urllib.request.build_opener(proxy_handler)
r = request.open('http://example.com')
print(r.read())



##################  二,使用requests。 #################

import requests

proxies = {
	'http': 'http://%s:%s'%(ip,port),
	'https': 'http://%s:%s'%(ip,port)
}

html = requests.get('http://ip.chinaz.com/',proxies=proxies)
html.encoding = 'utf-8'
print(html.text)


################### 也可以直接设置session的proxies属性,
################### 省得每次请求都要带上proxies参数。

sess = requests.session()
sess.proxies = proxies
print(sess.get('http://example.com').text)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值