Python3学习笔记:使用代理访问url地址
生活随笔
收集整理的这篇文章主要介绍了
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地址的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: DIV周边添加投影及背景固定
- 下一篇: Python多线程(1)——介绍