Python 3.x HTTP Error 403: Forbidden

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

 

转载于:https://www.cnblogs.com/szzshi/p/7388963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现urllib.error.HTTPError: HTTP Error 403: Forbidden的错误提示,通常是由于访问被拒绝所导致的。这可能是由于服务器拒绝了你的请求,或者你没有足够的权限来访问该资源。 针对这个问题,可以尝试以下几种解决方法: 1. 检查URL是否正确:确保你正在访问正确的URL,并且该URL确实存在。 2. 检查网络连接:确保你的网络连接正常,并且你可以访问其他网站。 3. 检查权限:如果你正在尝试访问需要权限的资源,请确保你具有足够的权限来访问该资源。 4. 检查代理设置:如果你正在使用代理服务器,请确保代理服务器的设置正确,并且你可以访问该代理服务器。 5. 检查防火墙设置:如果你正在使用防火墙,请确保防火墙的设置正确,并且允许你访问该资源。 针对引用中的问题,可以尝试以下解决方法: 1. 检查PPA源是否正确:确保你添加的PPA源是正确的,并且你可以访问该源。 2. 检查网络连接:确保你的网络连接正常,并且你可以访问其他网站。 3. 检查权限:如果你正在尝试访问需要权限的资源,请确保你具有足够的权限来访问该资源。 4. 检查代理设置:如果你正在使用代理服务器,请确保代理服务器的设置正确,并且你可以访问该代理服务器。 5. 检查防火墙设置:如果你正在使用防火墙,请确保防火墙的设置正确,并且允许你访问该资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值