Python "HTTP Error 403: Forbidden"
问题:
执行下面的语句时
def set_IPlsit():
url = 'https://www.whatismyip.com/'
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
出现以下异常:
C:\Users\54353\AppData\Local\Programs\Python\Python36\python.exe "C:/Users/54353/PycharmProjects/untitled/爬虫/图片 - 某网站.py"
Traceback (most recent call last):
File "C:/Users/54353/PycharmProjects/untitled/爬虫/图片 - 某网站.py", line 100, in <module>
ip = set_IPlsit2()
File "C:/Users/54353/PycharmProjects/untitled/爬虫/图片 - 某网站.py", line 95, in set_IPlsit2
response = ure.urlopen(url)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\54353\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden Process finished with exit code 1
分析:
出现上面的异常是因为用 urllib.request.urlopen 方式打开一个URL,服务器端只会收到一个单纯的对于该页面访问的请求,但是服务器并不知道发送这个请求使用的浏览器,操作系统,硬件平台等信息,而缺失这些信息的请求往往都是非正常的访问,例如爬虫。
有些网站为了防止这种非正常的访问,会验证请求信息中的UserAgent,如果UserAgent存在异常或者是不存在,那么这次请求将会被拒绝。
解决方法:
在请求中添加UserAgent,代码如下
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=chaper_url, headers=headers)
urllib.request.urlopen(req).read()
Python "HTTP Error 403: Forbidden"的更多相关文章
- python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)
抓取不得姐动图(报错) # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/7/23 17:01 imp ...
- urllib.error.HTTPError: HTTP Error 403: Forbidden
问题: urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出"HTTP Error 40 ...
- python3 HTTP Error 403:Forbidden
问题描述初学python,在用python中的urllib.request.urlopen()和urllib.request.urlretrieve方法打开网页时,有些网站会抛出异常: HTTP Er ...
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing
来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The ...
- 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing
git提交代码时,出现这个错误"error: The requested URL returned error: 403 Forbidden while accessing https&qu ...
- PYCURL ERROR 22 - "The requested URL returned error: 403 Forbidden"
RHEL6.5创建本地Yum源后,发现不可用,报错如下: [root@namenode1 html]# yum install gcc Loaded plugins: product-id, refr ...
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing(转)
github push错误: git push error: The requested URL returned error: 403 Forbidden while accessing https ...
- git推送到github报错:error: The requested URL returned error: 403 Forbidden while accessing https://github.com
最近使用git命令从github克隆仓库到版本,然后进行提交到github时报错如下: [root@node1 git_test]# git push origin mastererror: The ...
- python之urllib.request.urlopen(url)报错urllib.error.HTTPError: HTTP Error 403: Forbidden处理及引申浏览器User Agent处理
最近在跟着院内大神学习python的过程中,发现使用urllib.request.urlopen(url)请求服务器是报错: 在园子里找原因,发现原因为: 只会收到一个单纯的对于该页面访问的请求,但是 ...
随机推荐
- spring boot 使用拦截器 无法注入 配置值 、bean问题
参考:https://www.cnblogs.com/SimonHu1993/p/8360701.html 一定要将 拦截器组件 交给spring容器进行管理,这样才能注入配置值,或者注入bean: ...
- PCL法线估计
平面的法线是垂直于它的单位向量.在点云的表面的法线被定义为垂直于与点云表面相切的平面的向量.表面法线也可以计算点云中一点的法线,被认为是一种十分重要的性质.常常在被使用在很多计算机视觉的应用里面,比如 ...
- Linux下 mkdir 命令详解
一次性地建立多级目录,则可以使用-p参数 # mkdir -p /home/dir1/dir2/dir3
- Android 软键盘弹出与关闭监听
private void listenerSoftInput() { final View activityRootView = findViewById(R.id.activityRoot); ac ...
- SAP MM01 创建物料主数据 [关注公众号后回复MM01获取更多资料]
操作内容 物料主数据,适用于所有有物料编码物料相关信息的系统维护 业务流程 新项目设计冻结后—M公司 PD用-物料编码申请表D-BOM Material Number Application部门内部 ...
- 原创:如何实现在Excel通过循环语句设置指定行的格式
原创:如何实现在Excel通过循环语句设置指定行的格式 一.需求: 想让excel的某些行(比如3的倍数的行)字体变成5号字 如何整: 二.实现: Sub code() To Range(" ...
- MFC绘图小实验(2)
1,以正五边形的5个顶点为基础,隔点存储构成五角星.填充模式采用WINDING.五角星边界线为5个像素宽的蓝色实线,内部使用红色填充. CRect rect; //定义矩形 GetClientRect ...
- 第三百四十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP
第三百四十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP 设置代理ip只需要,自定义一个中间件,重写process_request方法, request ...
- Java如何格式化月份?
在Java中,如何以MMMM格式格式化时间? 这个示例使用SimpleDateFormat('MMMM')构造函数和SimpleDateFormat类的sdf.format(date)方法来格式化月份 ...
- e859. 将键盘事件和字符串对应
The KeyStroke.toString() method does not return a string that can be parsed by KeyStroke.getKeyStrok ...