|

楼主 |
发表于 2021-4-27 21:03:03
|
显示全部楼层
本帖最后由 591821661 于 2021-4-27 21:14 编辑
我不是水贴啊,分享干货 !
找到为啥时好时坏的原因了!!
一句话总结:
不要在ProxyHandler中使用127.0.0.1:80
因为之前有项目用了127.0.0.1:80 能正常运行 就没往这个方向考虑!
看来这个网站比较皮哦
proxy_handler = urllib.request.ProxyHandler({}) 正确写法
proxy_handler = urllib.request.ProxyHandler(
{
'http':'127.0.0.1:80',
'https':'127.0.0.1:80',
}
)
错误示范
测试代码
- # -*- coding: utf-8 -*-
- """
- Created on Tue Apr 27 19:32:49 2021
- @author: 591821661
- """
- import urllib.request
- import json
- import sys
- def useproxy(proxy=''):
- if proxy:
- proxy_support = urllib.request.ProxyHandler({
- 'http' : proxy,
- 'https' : proxy,
- })
- else:
- proxy_support = urllib.request.ProxyHandler({
- })
-
- opener = urllib.request.build_opener(proxy_support)
- opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'),
- # ('Host','ip-api.com'),
- # ('Connection','keep-alive'),
- # ('Upgrade-Insecure-Requests',1),
- # ('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'),
- # ('Accept-Encoding','gzip, deflate'),
- # ('Accept-Language','zh-CN,zh;q=0.9'),
- ]
- urllib.request.install_opener(opener)
-
- def get_ip():
- # Get IP Address
- try:
- ip_api_url = 'http://ip-api.com/json/?fields=61439'
- ip_result = urllib.request.urlopen(ip_api_url)
- proxy_text = ip_result.read().decode('utf-8')
- s_get_dict = json.loads(proxy_text)
- print('----------------------IP Information Begin--------------------------')
- for key in s_get_dict:
- print('%s : %s' % (key, s_get_dict[key]))
- print('----------------------IP Information End--------------------------')
- except:
- print('url_get error')
- print(sys.exc_info()[0], sys.exc_info()[1])
- if __name__ == '__main__': # 如果运行该文件,而不是作为库函数导入 则运行下列代码
- ip_api_url = 'http://ip-api.com/json/?fields=61439'
- useproxy() # 正常
- #useproxy('127.0.0.1:80') # 会报错
- get_ip()
复制代码 |
|