python 3请求模块的包装器
proxy-requests的Python项目详细描述
python代理请求使用从https://www.sslproxies.org/
PROXEXECUTE类首先从Web上擦除代理。然后,如果带有代理的初始请求不成功,它会递归地尝试发出请求。
复制代码并放在需要的地方,或者通过pip下载:
pip install proxy-requests
(或pip3)
from proxy_requests import ProxyRequests
或者如果您还需要基本的auth子类:
from proxy_requests import ProxyRequests, ProxyRequestsBasicAuth
如果使用上述import语句,则方法调用将与下面显示的相同。初始化实例时传递完全限定的URL。
系统需求:python 3和请求模块。
在Linux和Windows上运行(可能还有Mac)-根据当前的代理可能需要一些时间才能运行。
如果请求时间过长(在尝试队列中的下一个代理套接字之前),则每个带有代理的请求都会设置3秒超时。
proxyrequestbasicauth子类具有get()方法、get_with_headers()、post()、post_with_headers()、post_file()方法和post_file_with_headers()方法,这些方法将重写父方法。
获取:
r = ProxyRequests("https://api.ipify.org")
r.get()
使用标题:
h = {"User-Agent": "NCSA Mosaic/3.0 (Windows 95)"}
r = ProxyRequests("url here")
r.set_headers(h)
r.get_with_headers()
帖子:
r = ProxyRequests("url here")
r.post({"key1": "value1", "key2": "value2"})
带有标题的文章:
r = ProxyRequests("url here")
r.set_headers({"name": "rootVIII", "secret_message": "7Yufs9KIfj33d"})
r.post_with_headers({"key1": "value1", "key2": "value2"})
发布文件:
r = ProxyRequests("url here")
r.set_file({'file': open('test.txt', 'rb')})
r.post_file()
带有标题的post文件:
h = {"User-Agent": "NCSA Mosaic/3.0 (Windows 95)"}
r = ProxyRequests("url here")
r.set_headers(h)
r.set_file({'file': open('test.txt', 'rb')})
r.post_file_with_headers()
获得基本身份验证:
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.get()
使用标题获取和基本身份验证:
h = {"User-Agent": "NCSA Mosaic/3.0 (Windows 95)"}
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.set_headers(h)
r.get_with_headers()
使用基本身份验证发布:
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.post({"key1": "value1", "key2": "value2"})
带有标题和基本身份验证的帖子:
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.set_headers({"header_key": "header_value"})
r.post_with_headers({"key1": "value1", "key2": "value2"})
使用基本身份验证投递文件:
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.set_file({'file': open('test.txt', 'rb')})
r.post_file()
带有标题和基本身份验证的post文件:
h = {"User-Agent": "NCSA Mosaic/3.0 (Windows 95)"}
r = ProxyRequestsBasicAuth("url here", "username", "password")
r.set_headers(h)
r.set_file({'file': open('test.txt', 'rb')})
r.post_file_with_headers()
响应方法
返回字符串:print(r)
或者如果您希望原始内容是字节:r.get_raw()
将响应获取为json(如果是有效的json):r.get_json()
获取响应标题:print(r.get_headers())
获取状态代码:print(r.get_status_code())
获取用于发出请求的代理:print(r.get_proxy_used())
将原始数据A写入文件(包括图像):
url = 'https://www.restwords.com/static/ICON.png'
r = ProxyRequests(url)
r.get()
with open('out.png', 'wb') as f:
f.write(r.get_raw())
将响应转储为json:
import json
with open('test.txt', 'w') as file_out:
json.dump(r.get_json(), f)
这是在ubuntu 16.04.4lts上开发的。
作者:james loye colley 2018年8月4日