zoukankan      html  css  js  c++  java
  • python 爬虫随机获取User-Agent

      可以有两种方法:

      1、随机生成

        首先安装 pip install fake-useragent

       import random
        from fake_useragent import UserAgent
        def gererateUserAgent(self):
            # ua = UserAgent()
            # headers = {'User-Agent': ua.random}
            # print(headers)
            lst=[]
            for i in range(10):
                ua = UserAgent()
                lst.append(ua.random)
            print(lst)

      2、从列表中随机选择

    import random
    def getheaders():
        user_agent_list = ['Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',
                           'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36',
                           'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36',
                           'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
                           'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
                           'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
                           'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6',
                           'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36',
                           'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
                           'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36']
        UserAgent=random.choice(user_agent_list)

       3、查看google浏览器用户代理:

        在浏览器地址输入:chrome://version

      

        

  • 相关阅读:
    分支与循环(1)
    Python+selenium基本操作二
    python+selenium的八种定位方法
    变量与字符,数字连续
    Django虚拟环境拷贝到另一台电脑,不能直接使用的问题
    django部署到服务器使用manage.py runserver简单测试
    python中将main函数写成接口后main函数中的参数不能传递问题
    mac word2016尾部下划线不能显示
    简单的秒表实例
    Math.random理解练习
  • 原文地址:https://www.cnblogs.com/shaosks/p/10183919.html
Copyright © 2011-2022 走看看