爬蟲403問題解決urllib.error.HTTPError: HTTP Error 403: Forbidden

來源:互聯網
上載者:User

標籤:httperror: 403;urlretrieve

一、爬蟲時,出現urllib.error.HTTPError: HTTP Error 403: Forbidden

Traceback (most recent call last):  File "D:/訪問web.py", line 75, in <module>    downHtml(url=url)  File "D:/urllib訪問web.py", line 44, in downHtml    html=request.urlretrieve(url=url,filename=‘%s/%s.txt‘%(savedir,get_domain_url(url=url)))  File "C:\Python35\lib\urllib\request.py", line 187, in urlretrieve    with contextlib.closing(urlopen(url, data)) as fp:  File "C:\Python35\lib\urllib\request.py", line 162, in urlopen    return opener.open(url, data, timeout)  File "C:\Python35\lib\urllib\request.py", line 471, in open    response = meth(req, response)  File "C:\Python35\lib\urllib\request.py", line 581, in http_response    ‘http‘, request, response, code, msg, hdrs)  File "C:\Python35\lib\urllib\request.py", line 503, in error    result = self._call_chain(*args)  File "C:\Python35\lib\urllib\request.py", line 443, in _call_chain    result = func(*args)  File "C:\Python35\lib\urllib\request.py", line 686, in http_error_302    return self.parent.open(new, timeout=req.timeout)  File "C:\Python35\lib\urllib\request.py", line 471, in open    response = meth(req, response)  File "C:\Python35\lib\urllib\request.py", line 581, in http_response    ‘http‘, request, response, code, msg, hdrs)  File "C:\Python35\lib\urllib\request.py", line 509, in error    return self._call_chain(*args)  File "C:\Python35\lib\urllib\request.py", line 443, in _call_chain    result = func(*args)  File "C:\Python35\lib\urllib\request.py", line 589, in http_error_default    raise HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 403: Forbidden

二、分析:

  之所以出現上面的異常,是因為如果用 urllib.request.urlopen 方式開啟一個URL,伺服器端只會收到一個單純的對於該頁面訪問的請求,但是伺服器並不知道發送這個請求使用的瀏覽器,作業系統,硬體平台等資訊,而缺失這些資訊的請求往往都是非正常的訪問,例如爬蟲.有些網站為了防止這種非正常的訪問,會驗證請求資訊中的UserAgent(它的資訊包括硬體平台、系統軟體、應用軟體和使用者個人偏好),如果UserAgent存在異常或者是不存在,那麼這次請求將會被拒絕(如上錯誤資訊所示)所以可以嘗試在請求中加入UserAgent的資訊


三、方案:

對於Python 3.x來說,在請求中添加UserAgent的資訊非常簡單,代碼如下

#如果不加上下面的這行出現會出現urllib.error.HTTPError: HTTP Error 403: Forbidden錯誤  

    #主要是由於該網站禁止爬蟲導致的,可以在請求加上頭資訊,偽裝成瀏覽器訪問User-Agent,具體的資訊可以通過Firefox的FireBug外掛程式查詢 

headers = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36‘}  req = urllib.request.Request(url=chaper_url, headers=headers)  urllib.request.urlopen(req).read()

而使用request.urlretrieve庫下載時,如下解決:

opener=request.build_opener()opener.addheaders=[(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36‘)]request.install_opener(opener)request.urlretrieve(url=url,filename=‘%s/%s.txt‘%(savedir,get_domain_url(url=url)))

四、:

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8B/87/wKioL1hQzSHBTGRdAAGgQqt6C1U560.jpg-wh_500x0-wm_3-wmp_4-s_1239733674.jpg" title="3.jpg" alt="wKioL1hQzSHBTGRdAAGgQqt6C1U560.jpg-wh_50" />


爬蟲403問題解決urllib.error.HTTPError: HTTP Error 403: Forbidden

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.