码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3.x HTTP Error 403: Forbidden

时间:2017-08-18 13:30:03      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:err   tps   head   webp   http   sys   exp   sub   instr   

The Fobidden error often raised when using request.open to open some urls.

such as:

url_1 = ‘https://movie.douban.com/subject/26363254/comments?status=P‘
url_2 = ‘https://www.glassdoor.com/Interview/Texas-Instruments-Interview-Questions-E651_P4.htm‘

request.urlopen(url_1) # ** no error raised

request.urlopen(url_2) # ** Fobidden error raised

Here is the reason:

When using urllib.request.urlopen to visit a URL, the server will only receive a simple request for this webpage without knowing the hidden infos about exploer,operating system,platform, which are abnormal. 

Some websited will vefify the UserAgent info to prevent the abnoraml visisting. 

So the solution : Add these infos to the UserAgent to acts as using exploer to visit

req = request.Request(url,headers={‘User-Agent‘: ‘Mozilla/5.0‘}) # ** this would fix, also you can add other infos to User-Agent

 

Python 3.x HTTP Error 403: Forbidden

标签:err   tps   head   webp   http   sys   exp   sub   instr   

原文地址:http://www.cnblogs.com/szzshi/p/7388963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!