使用Request伪装User-Agent和IP地址
使用urllib伪装User-Agent:https://www.cnblogs.com/qyly/p/18410791
,https://www.cnblogs.com/qyly/p/18415401
使用urllib伪装IP地址:https://www.cnblogs.com/qyly/p/18416308
接下来使用requests库替代urllib完成伪装工作,继续用httpbin测试工具查看响应结果:
运行代码:
import requests
from fake_useragent import UserAgent
def test_headers():
# 请求地址
url = 'http://httpbin.org/get'
# 封装伪装的请求头
headers = {'User-Agent': UserAgent().ff}
# 发送请求
resp = requests.get(url, headers=headers)
# 读取响应结果的文本内容
print(resp.text)
def test_proxy():
# 设置代理IP
# proxy = {'type': 'ip:port'}
proxy = {'http': '47.106.208.135:7777'}
# 请求地址
url = 'http://httpbin.org/get'
# 封装伪装的请求头
headers = {'User-Agent': UserAgent().ff}
# 发送请求
resp = requests.get(url, headers=headers, proxies=proxy)
# 读取响应结果的文本内容
print(resp.text)
if __name__ == '__main__':
test_headers() # 伪装User-Agent
test_proxy() # 伪装User-Agent和IP地址
运行结果:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0",
"X-Amzn-Trace-Id": "Root=1-66ef6916-3dd3ddef0da48199207fc3a0"
},
"origin": "111.8.72.145",
"url": "http://httpbin.org/get"
}
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0",
"X-Amzn-Trace-Id": "Root=1-66ef692f-170105a707b577100f986e22"
},
"origin": "47.106.208.135",
"url": "http://httpbin.org/get"
}