码迷,mamicode.com
首页 > 其他好文 > 详细

设置随机请求头和使用代理

时间:2020-01-04 18:52:24      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:load   aci   nts   res   and   ext   ati   ida   sts   

# -*- 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()




设置随机请求头和使用代理

标签:load   aci   nts   res   and   ext   ati   ida   sts   

原文地址:https://www.cnblogs.com/zxfei/p/12149655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!