第一种:

    def get_ua(self):
        first_num = random.randint(55, 62)
        third_num = random.randint(0, 3200)
        fourth_num = random.randint(0, 140)
        os_type = [
            '(Windows NT 6.1; WOW64)', '(Windows NT 10.0; WOW64)', '(X11; Linux x86_64)',
            '(Macintosh; Intel Mac OS X 10_12_6)'
        ]
        chrome_version = 'Chrome/{}.0.{}.{}'.format(first_num, third_num, fourth_num)
        ua = ' '.join(['Mozilla/5.0', random.choice(os_type), 'AppleWebKit/537.36',
                       '(KHTML, like Gecko)', chrome_version, 'Safari/537.36']
                      )
        return ua

第二种:

pip install fake-useragent

from fake_useragent import UserAgent
ua=UserAgent()
print(ua.random)
版权属于:it小离

本文链接: https://www.itxiaoli.cn/archives/useragent.html

版权声明:本站文章采用 署名-非商业性使用-相同方式共享 4.0

最后修改:2022 年 03 月 25 日 07 : 14 PM
如果觉得我的文章对你有用,请随意赞赏