日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python3学习笔记:使用代理访问url地址

發布時間:2025/7/14 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3学习笔记:使用代理访问url地址 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

#!?/usr/bin/env?python3 #?-*-?coding:utf-8?-*-'python進行代理的curl數據提交'__author__?=?'ken'import?os; import?sys;curPath?=?os.path.abspath(os.path.dirname(__file__)); sys.path.append(curPath);import?urllib.request; import?urllib.parse; import?socket;class?curl:def?__init__(self):pass;#?獲取用戶瀏覽器信息def?getUserAgent(self):userAgent?=?'Mozilla/5.0?(Windows?NT?6.1;?WOW64;?rv:39.0)?Gecko/20100101?Firefox/39.0';return?userAgent;#?進行數據提交def?run(self,?url,?param):self.url?=?url;self.param?=?param;self.userAgent?=?self.getUserAgent();self.proxyIpList?=?['117.135.196.197:55336',?'117.158.98.214:80',?'117.177.243.42:84',?'117.177.243.42:85'];data?=?urllib.parse.urlencode(self.param).encode(encoding='UTF8');req?=?urllib.request.Request(self.url,?data);req.add_header('User-Agent',?self.userAgent);for?proxyIp?in?self.proxyIpList:socket.setdefaulttimeout(3);??#?3秒未響應則為超時,跳過執行下一條try:#?添加代理proxy_handler?=?urllib.request.ProxyHandler({'http':?proxyIp});proxy_auth_handler?=?urllib.request.ProxyBasicAuthHandler();opener?=?urllib.request.build_opener(proxy_handler,?proxy_auth_handler);#?添加頭信息opener.addheaders?=?[('User-Agent',?self.userAgent)]#?數據請求response?=?opener.open(self.url,?data);#?獲取請求返還數據response_data?=?response.read().decode("utf8");print(proxyIp,?"正確:"?+?response_data);#?return?response_data;except?urllib.error.HTTPError?as?e:print(proxyIp,?"錯誤:錯誤代碼:",?e.code);#?print("錯誤內容:",?e.read().decode("utf8"));except?urllib.error.URLError?as?e:print(proxyIp,?'錯誤:未能獲取服務器信息.');#?print('錯誤原因:?',?e.reason);except:print(proxyIp,?"錯誤:其他未知錯誤!");#?cu?=?curl(); #?cu.run("www.test.com","{"key":123456789}");


轉載于:https://my.oschina.net/kenblog/blog/486510

總結

以上是生活随笔為你收集整理的Python3学习笔记:使用代理访问url地址的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。