1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from fake_useragent import UserAgent   # 下载:pip install fake-useragent
import requests

ua = UserAgent() # 实例化,需要联网但是网站不太稳定-可能耗时会长一些
print(ua.random) # 随机产生
headers = {
'User-Agent': ua.random # 伪装
}
print(headers)
# 请求
if __name__ == '__main__':
url = 'https://www.baidu.com/'
response = requests.get(url, headers=headers ,proxies={"http":"117.136.27.43"})
print(response.status_code)

1
2
3
4
5
6
7
8
proxies = {
"http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": "t18552632261285", "pwd": "bjnrabj3", "proxy": "a892.kdltps.com:15818"},
# "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": "t18637964583060", "pwd": "7wu91jnb", "proxy": "s1000.kdltps.com:15818"},
}


resp = requests.get(url, headers=headers,proxies=proxies)
print(resp.status_code)