首页 > 编程语言 >python爬虫随机headers伪装fake_useragent

python爬虫随机headers伪装fake_useragent

时间:2022-09-24 13:22:47浏览次数:52  
标签:__ python headers 随机 fake useragent ua

python爬虫随机headers伪装fake_useragent

fake_useragent 库

调用方法ua.random可以随机返回一个headers(User-Agent)

from fake_useragent import UserAgent   # 下载:pip install fake-useragent
import requests

ua = UserAgent()        # 实例化,需要联网但是网站不太稳定-可能耗时会长一些
print(ua.random)  # 随机产生
headers = {
    'User-Agent': ua.random    # 伪装
    }

# 请求
if __name__ == '__main__':
    url = 'https://www.baidu.com/'
    response = requests.get(url, headers=headers ,proxies={"http":"117.136.27.43"})
    print(response.status_code)

标签:__,python,headers,随机,fake,useragent,ua
From: https://www.cnblogs.com/rainbow-1/p/16725473.html

相关文章

  • 让 Python 社区活跃起来的修复
    让Python社区活跃起来的修复在Python的整数字符串转换中发现的漏洞的“修复”一直是Python社区讨论的主题。Photoby阿图里·贾利on不飞溅如您所知,整数数据......
  • python安装库的时候产生'check_hostname requires server_hostname'解决方法
    pipinstallxxx的时候报错...   raiseValueError("check_hostnamerequiresserver_hostname")ValueError:check_hostnamerequiresserver_hostname换源#清华源pi......
  • Python解释器下载与安装
    Python解释器下载与安装一、Python解释器1.Python的发展方向web方向,自动化运维,自动化测试,自动化办公,网络爬虫,金融量化,人工智能,机器学习,数据分析2.Python解释器版本Pyt......
  • Python pandas Dataframe sort_values 无效
    一定要注意df=df.sort_values()这个函数不是原地改变数据的同时也要主注意,在文档里虽然是这么写的,但是jupyter环境打印的是返回值......
  • 使用 Python 3 开发聊天机器人
    使用Python3开发聊天机器人我最近登陆了一个小项目,使用Python3开发了一个在Telegram平台上使用的聊天机器人。您可以查看机器人帐户@https://t.me/AI_12_Bot.......
  • windows平台下sublime配置python开发环境
    目录1,下载与安装2,包管理的安装3,sublime软件环境配置3,编译&运行1,下载与安装1.下载(官网http://www.sublimetext.com/download)并安装即可。2,包管理的安装1.将安装后的b......
  • 有效地对 Python 模块导入进行排序
    有效地对Python模块导入进行排序在本文中,我们将了解如何使用isort库来自动安排Python模块的导入。随着Python项目的扩展,您开始拥有越来越多的文件,每个文件都包......
  • 大量的 Python 阅读
    Pythonlogobywww.python.org,GPL,via维基共享资源大量的Python阅读来自ThePragmaticProgrammers的Python内容https://pragprog.com/newsletter/Pyt......
  • python 3.10 unicode \U 转中文
    python3.10unicode\U转中文`str="\u574f\u86cb"print(str.encode('utf-8').decode('unicode_escape'))importjsoncc=json.dumps(str).encode('utf-8').decode(......
  • python-json
    pythonjson处理json写入json文件点击查看代码importjsonjson_data={"name":"Tom","age":18,"score":{"math":98,"chines......