win10下python3.9的代理报错问题解决(附web3的polygon爬虫源码)
2023-01-19 14:03 第二个卿老师 阅读(252) 评论(0) 编辑 收藏 举报背景
因为工作中经常需要代理访问,而开了代理,request就会报错SSLError,如下:
requests.exceptions.SSLError: HTTPSConnectionPool(host='test-admin.xxx.cn', port=443): Max retries exceeded with url: /xxx/xxx/xxx (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)')))
具体原因是python
自身的 urllib
库没有正确配置 HTTPS 代理,参考链接(我本地环境是win10,python3.9)
如何解决
当然最简单的方式是手动关闭代理,如何开着代理也ok呢,如下所示
1 2 | proxies = { "http" : None , "https" : 'http://127.0.0.1:7890' } # 设置本地代理 res = requests.get(url = self .url, params = self .params, proxies = proxies) |
附:polygon爬虫源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import math import requests from bs4 import BeautifulSoup class Polygon(): url = 'https://polygonscan.com/txs' headers = { 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36' } var_trans_list = [] def __init__( self ): global mian_address, proxies mian_address = '0x4C59739D4B43cc9b8C81d959F93e4cfD69736244' # input("请输入账号地址:") proxies = { "http" : None , "https" : 'http://127.0.0.1:7890' } # 设置本地代理 self .params = { 'a' : mian_address } self .res = '' page = self .is_paging() if page> 1 : print (f '共有{page}页数据' ) for p in range ( 1 , page + 1 ): self .params = { 'a' : mian_address, 'p' : p } print (f '正在获取第{p}页。。。' ) self .find_data() else : self .find_data() print (f '全部数据共{len(self.var_trans_list)}条,为{self.var_trans_list}' ) # 调用接口获取源数据 def catch_polygon( self ): self .res = requests.get(url = self .url, params = self .params, proxies = proxies) if self .res.status_code = = 200 : res_utf8 = self .res.content.decode( "utf-8" ) self .res_html = BeautifulSoup(res_utf8, 'html.parser' ) else : print ( '抓取页面失败' ) # 是否存在分页 def is_paging( self ): self .catch_polygon() new_reslut = self .res_html.find( class_ = 'mb-2 mb-md-0' ) if new_reslut: list = new_reslut.text.split( ' ' ) if int ( list [ 3 ]) > = 0 : print (f "一共查询到{list[3]}条数据" ) return math.ceil(( int ( list [ 3 ]) + 1 ) / 50 ) # 过滤匹配数据 def find_data( self ): list2 = [] self .catch_polygon() new_reslut = self .res_html.find( class_ = 'table table-hover' ).find_all( 'tr' ) for i in new_reslut: list = [] for j in i: if j.text: list .append(j.text) list2.append( list ) if list2: list2 = list2[ 1 :] # 去除第一行无效数据 print (f "当页共{len(list2)}条数据" ) self .var_trans_list.extend(list2) else : print ( '没有查询到数据' ) if __name__ = = '__main__' : Polygon() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现