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

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

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

时间: 2024-08-26 05:29:54

设置随机请求头和使用代理的相关文章

Scrapy 增加随机请求头 user_agent

原文: 为什么要增加随机请求头:更好地伪装浏览器,防止被 Ban. 如何在每次请求时,更换不同的 user_agent,Scrapy 使用 Middleware 即可 Spider 中间件 (Middleware) 下载器中间件是介入到 Scrapy 的 spider 处理机制的钩子框架,可以添加代码来处理发送给 Spiders 的 response 及 spider 产生的 item 和 request. 官网说明在这里:Spider Middleware 添加 middleware 的步骤:

selenium.Phantomjs设置浏览器请求头

from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #设置浏览器请求头 dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] =( "Mozilla/5.0 (Linux; Android 5.1

js 中ajax请求时设置 http请求头中的x-requestd-with= ajax

今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法判断 接受的http请求是否是 ajax请求了. 怎么办呢,显然就是给http 请求头中 加上这个字段就可以了. 1.AngularJS 中可以这样子: I don't know well MVC3 but you can set a custom header for all request fr

angular学习笔记(二十四)-$http(2)-设置http请求头

1. angular默认的请求头: 其中,Accept 和 X-Requested-With是$http自带的默认配置 2. 修改默认请求头: (1) 全局修改(整个模块) 使用$httpProvider依赖 var myApp = angular.module('MyApp',[]); myApp.config(function($httpProvider){ console.log($httpProvider.defaults.headers.common) //修改/操作$httpProv

接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

转自:https://www.cnblogs.com/hong-fithing/p/7617855.html https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, HTTPS以保密为目标研发, 简单讲HTTPS协议是由SSL+HTTP协议构建的可进行加密传输. 身份认证的网络协议, 其安全基础是SSL协议, 因此加密的详细内容请看SSL. 全称Hypertext Transfer Protocol overSecure

Retrofit2 + OkHttp3设置Http请求头(Headers)方法汇总

在构建网络层时会遇到一个问题就是要手动配置Http请求的Headers,写入缓存Cookie,自定义的User-Agent等参数,但是对于有几十个接口的网络层,我才不想用注解配置Headers,目前网上很多文章的方法真对这两个版本都不是很适用,有的给出的方法已经被删除,有的方法会报出异常 :( 方法一: 在翻阅官方API文档整理后的方法如下: 1.  import okhttp3.Interceptor; 2.  import okhttp3.OkHttpClient; 3.  import o

php curl如何设置自定义请求头和打印请求头信息

$header = [ 'client:h5', 'token:test', ]; curlRequest($url, $params, true, 10, $header); PHP 5.1.3版以上支持用curl_getinfo函数来获取请求头具体需要先设置 curl_setopt($ch, CURLINFO_HEADER_OUT, true);然后在请求发生后用 curl_getinfo($ch, CURLINFO_HEADER_OUT); function curlRequest($ur

ajax设置自定义请求头信息

客户端请求 $.ajax({ type:"post", url:urlstr, dataType:'json', async:true, headers:{token:'abck'}, success:function (data) { console.log(data.info); console.log(JSON.stringify(data)); }, error:function (xhr,text) { alert(text); } }); 服务端代码 public func

ajax设置自定义请求头

1, $.ajax({ url:apiUrl, type:"get", timeout : 5000, //超时时间设置,单位毫秒 dataType: "json", beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Bearer "+obj.data); xhr.setRequestHeader("Accept", &qu