马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import urllib.request
#这一段三句话是为了请求时带上浏览器标识,因为有的网站看到是爬虫的标识直接返回403
#请求的网站不涉及到提交数据,所以没有给出data参数
url = 'https://baidu.com/'
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-US,en;q=0.5',
'Accept-Encoding':'gzip',
'DNT':'1',
'Connection':'close'}
req = urllib.request.Request(url,headers=header)
#使用代理和还原不使用代理的方法
#if语句相当于一个开关,不要写成True
use_proxy = urllib.request.ProxyHandler({'https':'59.38.222.104'})
opener = urllib.request.build_opener(use_proxy)#默认使用本地ip
urllib.request.install_opener(opener)
#获取返回结果
#同时可以使用html = opener.open(req).read()获取结果
html = urllib.request.urlopen(req).read()
print(html)
为什么我找了好多代理ip试了,都不行,我都开始怀疑是我程序有问题了。
大佬们看看程序有没有问题?然后再推荐推荐几个代理ip网址呗 |