设置随机请求头和使用代理
# -*- coding: utf-8 -*- """ 所有请求头的USER_AGENTS网址 http://www.useragentstring.com/pages/useragentstring.php?name=All """ import json import random import requests USER_AGENTS = [ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/44.0.2403.155 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36 Chrome 41.0.2227.0', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36', ] #从列表中随机选择一个 User_Agent = random.choice(USER_AGENTS) headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', 'User-Agent': User_Agent } def testUserAgent(): resp = requests.get("http://httpbin.org/user-agent", headers=headers) user_agent = json.loads(resp.text)['user-agent']#将返回的字符串loads为字典 print(user_agent) def testProxy(): proxies = {"https": "223.199.25.183:9999"} resp = requests.get("http://httpbin.org/ip", proxies=proxies) origin = json.loads(resp.text)['origin'] print(origin) # https://www.xicidaili.com/nn/ 西刺代理 if __name__ == "__main__": testUserAgent() testProxy()
相关推荐
liuweiq 2020-07-08
ZHANGRENXIANG00 2020-06-09
88961137 2020-06-01
marisafari 2020-05-29
jiaguoquan00 2020-05-27
fanhuasijin 2020-04-17
juanjuanwang 2020-03-23
81224450 2020-01-24
程序员俱乐部 2020-01-11
Safari浏览器 2019-12-14
88961137 2019-12-09
marisafari 2019-10-26
longzhiwen 2020-10-16
tkernel 2020-08-18
lihn 2020-08-18
Colourful 2020-07-03
sunzhihaofuture 2020-06-10
fangjack 2020-06-02