在使用 Python 做爬蟲的時候,我們需要偽裝頭部信息騙過網站的防爬策略,Python 中的第三方模塊 fake_useragent 就很好的解決了這個問題,它將給我們返回一個隨機封裝了好的頭部信息,我們直接使用即可
fake_useragent 的使用
安裝 fake_useragent
pip install fake_useragent
示例:
from fake_useragent import UserAgent
# 實例化 UserAgent 類
ua = UserAgent()
# 對應瀏覽器的頭部信息
print(ua.ie)
print(ua.opera)
print(ua.chrome)
print(ua.firefox)
print(ua.safari)
# 隨機返回頭部信息,推薦使用
print(ua.random)
運行結果:
(adnice) adnice:Downloads zhangyi$ python3 fake.py
Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; SLCC1; .NET CLR 1.1.4322)
Opera/9.80 (Windows NT 6.1; U; fi) Presto/2.7.62 Version/11.00
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1500.55 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:23.0) Gecko/20131011 Firefox/23.0
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2117.157 Safari/537.36
fake_useragent 報錯及解決方案
報錯信息:
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\programdata\anaconda3\lib\site-packages\fake_useragent\utils.py", lin
e 166, in load
verify_ssl=verify_ssl,
File "d:\programdata\anaconda3\lib\site-packages\fake_useragent\utils.py", lin
e 122, in get_browser_versions
verify_ssl=verify_ssl,
File "d:\programdata\anaconda3\lib\site-packages\fake_useragent\utils.py", lin
e 84, in get
raise FakeUserAgentError('Maximum amount of retries reached')
fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
首先找出關鍵報錯信息:
fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
大概意思是:這個模塊嘗試請求一個東西已達到最大重試次數
打開這個模塊的源碼進行查看發現這個庫會引用在線資源,所以這個模塊是進行幾次嘗試請求一個網站的 Json 數據,但是因為各種原因請求超時,所以就會報這個錯誤
fake_useragent\settings.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import os
import tempfile
__version__ = '0.1.11'
DB = os.path.join(
tempfile.gettempdir(),
'fake_useragent_{version}.json'.format(
version=__version__,
),
)
CACHE_SERVER = 'https://fake-useragent.herokuapp.com/browsers/{version}'.format(
version=__version__,
)
BROWSERS_STATS_PAGE = 'https://www.w3schools.com/browsers/default.asp'
BROWSER_BASE_PAGE = 'http://useragentstring.com/pages/useragentstring.php?name={browser}' # noqa
BROWSERS_COUNT_LIMIT = 50
REPLACEMENTS = {
' ': '',
'_': '',
}
SHORTCUTS = {
'internet explorer': 'internetexplorer',
'ie': 'internetexplorer',
'msie': 'internetexplorer',
'edge': 'internetexplorer',
'google': 'chrome',
'googlechrome': 'chrome',
'ff': 'firefox',
}
OVERRIDES = {
'Edge/IE': 'Internet Explorer',
'IE/Edge': 'Internet Explorer',
}
HTTP_TIMEOUT = 5
HTTP_RETRIES = 2
HTTP_DELAY = 0.1
解決方案:
首先第一步要進行更新 fake_useragent
pip install --upgrade fake_useragent
1. 在實例化的時候指定一些參數
禁用服務器緩存
ua = UserAgent(use_cache_server=False)
不緩存數據
ua = UserAgent(cache=False)
忽略ssl驗證
ua = UserAgent(verify_ssl=False)
一般的話,通過上述解決方案都能解決了,但是我就比較悲催了,還是沒解決…
2. 使用臨時 Json 文件
在 fake_useragent\settings.py 發現了幾個 URL,其中有一些是打不開的,所以,我們將能打開的 URL 的 Json 文件保存在本地
wget https://fake-useragent.herokuapp.com/browsers/0.1.11
這時我們就會得到一個 0.1.11 的文件,將文件名改為 fake_useragent_0.1.11.json
mv 0.1.11 fake_useragent_0.1.11.json
然后找到我們的臨時文件目錄(每個系統都不一樣,例如 Ubuntu 在 /tmp 下)
(edison) adnice:T zhangyi$ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.gettempdir()
'/var/folders/6_/p67xz49j5wd5lzx7s2cz1cdr0000gn/T'
>>>
最后將文件拷貝到臨時目錄中即可
cp fake_useragent_0.1.11.json /var/folders/6_/p67xz49j5wd5lzx7s2cz1cdr0000gn/T/
當我們再次實例化 UserAgent 的時候,就會先讀取本地的臨時文件,這樣實例化的時候就不會報錯了
參考文章:https://blog.csdn.net/huiyanshizhu/article/details/84952093
————————————————
版權聲明:本文為CSDN博主「極客點兒」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/yilovexing/article/details/89044980