Python爬蟲隨機UserAgent庫,讓你不再手動敲UA!
前言
之前也懵懵懂懂寫過python爬蟲,但是經常被網站的反爬機制幹趴下,然後手動寫了個隨機UA庫,情況才好些。今天在網際網路暢遊時發現,有一個能夠產生隨機UA的第三方庫!
安裝第三方庫
老生常談啦,pip安裝執行以下程式碼即可~
pip install fake-useragent
簡單上手
簡單上手很簡單的,上程式碼~
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.random)
# 輸出:Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4
ua.random就能產生一個隨機UA了!這不禁讓我想起了以前手動複製數個UA,然後再隨機選擇的程式碼…程式碼量蹭蹭的就下去了。
幾十行->三行
正好體現了Python的簡潔之美!
擴充(中文幫助文件)
fake-useragent: github
作者的資訊概要也很精簡:
Up to date simple useragent faker with real world database
幫助文件:
fake-useragent
將會把收集到的資料儲存在你的系統臨時資料夾中,比如/temp
如果你想更新已有的資料庫只需要這樣:
from fake_useragent import UserAgent
ua = UserAgent()
ua.update()
如果你不想快取資料庫或者不生成寫入檔案:
from fake_useragent import UserAgent
ua = UserAgent(cache=False)
有時候, useragentstring.com or w3schools.com 更改他們的網址或者關閉網站,在這種情況下,fake-useragent
將會使用 heroku 的返回結果。
如果你不想使用託管快取伺服器:
from fake_useragent import UserAgent
ua = UserAgent(use_cache_server=False)
在非常罕見的情況下,如果託管的快取伺服器和源不可用,fake-useragent將無法下載資料:
from fake_useragent import UserAgent
ua = UserAgent()
# Traceback (most recent call last):
# ...
# fake_useragent.errors.FakeUserAgentError
# You can catch it via
from fake_useragent import FakeUserAgentError
try:
ua = UserAgent()
except FakeUserAgentError:
pass
相關文章
- Python爬蟲教程-10-UserAgent和常見瀏覽器UA值Python爬蟲瀏覽器
- C#爬蟲,讓你不再覺得神祕C#爬蟲
- 爬蟲抓取UserAgent問題爬蟲
- 騰訊動漫爬蟲與動態隨機載入反爬爬蟲隨機
- python爬蟲從ip池獲取隨機IPPython爬蟲隨機
- @程式設計師,一文讓你掌握Python爬蟲!程式設計師Python爬蟲
- 騰訊動漫爬蟲與動態隨機載入反爬破解技術實戰爬蟲隨機
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- Python爬蟲入門教程 48-100 使用mitmdump抓取手機惠農APP-手機APP爬蟲部分Python爬蟲MITAPP
- Python爬蟲之BeautifulSoup庫Python爬蟲
- 19個Python爬蟲專案讓你一次吃到撐Python爬蟲
- 32個Python爬蟲專案讓你一次吃到撐Python爬蟲
- 【python爬蟲】python爬蟲demoPython爬蟲
- python爬蟲之反爬蟲(隨機user-agent,獲取代理ip,檢測代理ip可用性)Python爬蟲隨機
- Python爬蟲建站入門手記(3):採集入庫Python爬蟲
- Python爬蟲入門教程 61-100 寫個爬蟲碰到反爬了,動手破壞它!Python爬蟲
- python爬蟲庫技術分享Python爬蟲
- python爬蟲之一:requests庫Python爬蟲
- Python爬蟲新手教程:手機APP資料抓取 pyspiderPython爬蟲APPIDE
- python爬蟲總是爬不到資料,你需要解決反爬蟲了Python爬蟲
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站
- (python)爬蟲----八個專案帶你進入爬蟲的世界Python爬蟲
- 帶你入門Python爬蟲,8個常用爬蟲技巧盤點Python爬蟲
- Python 從零開始爬蟲(六)——動態爬取解決方案 之 手動分析Python爬蟲
- Python3網路爬蟲(十一):爬蟲黑科技之讓你的爬蟲程式更像人類使用者的行為(代理IP池等)Python爬蟲
- python爬蟲 之 BeautifulSoup庫入門Python爬蟲
- 小白學python-爬蟲常用庫Python爬蟲
- python就是爬蟲嗎-python就是爬蟲嗎Python爬蟲
- Emmet,讓你愛上敲程式碼
- Python爬蟲Python爬蟲
- python 爬蟲Python爬蟲
- 資源整理 | 32個Python爬蟲專案讓你一次吃到撐Python爬蟲
- 資源整理 |19個Python爬蟲專案讓你一次吃到撐Python爬蟲
- python爬蟲解決趕集網掃碼獲取手機號Python爬蟲
- Python自動化爬取小說,解放你的雙手Python
- 讓你的應用程式不再對資料庫的改動“感冒”(三) (轉)資料庫
- 讓你的應用程式不再對資料庫的改動“感冒”(一) (轉)資料庫
- 讓你的應用程式不再對資料庫的改動“感冒”(二) (轉)資料庫