Python爬蟲之UserAgent的使用實例
問題: 在Python爬蟲的過程中經(jīng)常要模擬UserAgent, 因此自動生成UserAgent十分有用, 最近看到一個Python庫(fake-useragent),可以隨機生成各種UserAgent, 在這里記錄一下, 留給自己爬蟲使用。
安裝 pip install fake-useragent
使用案例
基本使用
from fake_useragent import UserAgent ua = UserAgent() ua.ie # Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US); ua.msie # Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)' ua['Internet Explorer'] # Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US) ua.opera # Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11 ua.chrome # Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2' ua.google # Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13 ua['google chrome'] # Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11 ua.firefox # Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1 ua.ff # Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1 ua.safari # Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25 # and the best one, random via real world browser usage statistic ua.random
注意:
fake-useragent 將收集到的數(shù)據(jù)緩存到temp文件夾, 例如 /tmp, 更新數(shù)據(jù):
from fake_useragent import UserAgent ua = UserAgent() ua.update()
有時候會因為網(wǎng)絡(luò)或者其他問題,出現(xiàn)異常(fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached), 可以禁用服務(wù)器緩存(從這里踩了一個坑, 沒仔細看文檔的鍋):
from fake_useragent import UserAgent ua = UserAgent(use_cache_server=False)
可以自己添加本地數(shù)據(jù)文件(v0.1.4+)
import fake_useragent # I am STRONGLY!!! recommend to use version suffix location = '/home/user/fake_useragent%s.json' % fake_useragent.VERSION ua = fake_useragent.UserAgent(path=location) ua.random
其他功能用到的也不是很多,詳細見文檔吧。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Nginx服務(wù)器屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲的方法
- Python爬蟲beautifulsoup4常用的解析方法總結(jié)
- Python常用爬蟲代碼總結(jié)方便查詢
- Python 通過requests實現(xiàn)騰訊新聞抓取爬蟲的方法
- Python3爬蟲之自動查詢天氣并實現(xiàn)語音播報
- 基于node.js實現(xiàn)爬蟲的講解
- 淺談Scrapy網(wǎng)絡(luò)爬蟲框架的工作原理和數(shù)據(jù)采集
- 用Electron寫個帶界面的nodejs爬蟲的實現(xiàn)方法
- 通過python爬蟲賺錢的方法
- 如何禁止網(wǎng)站內(nèi)容被搜索引擎收錄的幾種方法講解
相關(guān)文章
Python:Scrapy框架中Item Pipeline組件使用詳解
這篇文章主要介紹了Python:Scrapy框架中Item Pipeline組件使用詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
python利用smtplib實現(xiàn)QQ郵箱發(fā)送郵件
這篇文章主要為大家詳細介紹了python利用smtplib實現(xiàn)QQ郵箱發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情
這篇文章主要介紹了PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情,首先手動連接數(shù)據(jù)庫與下一個的程序連接數(shù)據(jù)庫是獨立的2個部分,下面來看看文章的詳細介紹2022-01-01
python格式化字符串的實戰(zhàn)教程(使用占位符、format方法)
我們經(jīng)常會用到%-formatting和str.format()來格式化,下面這篇文章主要給大家介紹了關(guān)于python格式化字符串的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
Python 用NumPy創(chuàng)建二維數(shù)組的案例
這篇文章主要介紹了Python 用NumPy創(chuàng)建二維數(shù)組的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Numpy中的數(shù)組搜索中np.where方法詳細介紹
這篇文章主要介紹了Numpy中的數(shù)組搜索中np.where方法詳細介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01

