python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)
抓取不得姐动图(报错)
# -*- coding:utf-8 -*-
#__author__ :kusy
#__content__:文件说明
#__date__:2018/7/23 17:01
import urllib.request
import re def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
# print(html)
return html def getImg(reg,savePath):
iCnt = 0
def giveImg(html):
imgre = re.compile(reg)
imglist = re.findall(imgre, html.decode('utf-8'))
nonlocal iCnt
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, savePath + '%s.gif' % iCnt)
iCnt += 1
return giveImg # html = getHtml("http://pic.sogou.com/")
# reg = r'"image":"(.+?)"' #sougou reg = r'data-original="(.+?\.gif)"'
savePath = 'image/gif/'
g = getImg(reg,savePath)
for i in range(10):
if i >1:
print("http://www.budejie.com/" + str(i))
html = getHtml("http://www.budejie.com/" + str(i))
else:
html = getHtml("http://www.budejie.com/")
g(html)
报错如下
E:\kusy\python\venv\Scripts\python.exe E:/kusy/python/getJpg.py
http://www.budejie.com/2
Traceback (most recent call last):
File "E:/kusy/python/getJpg.py", line 35, in <module>
html = getHtml("http://www.budejie.com/" + str(i))
File "E:/kusy/python/getJpg.py", line 9, in getHtml
page = urllib.request.urlopen(url)
File "C:\Users\jingjing\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\jingjing\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\jingjing\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\jingjing\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\jingjing\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\jingjing\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
百度了下已解决:
# -*- coding:utf-8 -*-
#__author__ :kusy
#__content__:文件说明
#__date__:2018/7/23 17:01
import urllib.request
import re def getHtml(url):
# 如果不加上下面的这行出现会出现urllib.error.HTTPError: HTTP Error 403: Forbidden错误
# 主要是由于该网站禁止爬虫导致的,可以在请求加上头信息,伪装成浏览器访问User-Agent,具体的信息可以通过火狐的FireBug插件查询
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=url,headers=headers)
page = urllib.request.urlopen(req)
html = page.read()
# print(html)
return html def getImg(reg,savePath):
iCnt = 0
def giveImg(html):
imgre = re.compile(reg)
imglist = re.findall(imgre, html.decode('utf-8'))
nonlocal iCnt
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, savePath + '%s.gif' % iCnt)
iCnt += 1
return giveImg # html = getHtml("http://pic.sogou.com/")
# reg = r'"image":"(.+?)"' #sougou reg = r'data-original="(.+?\.gif)"'
savePath = 'image/gif/'
g = getImg(reg,savePath)
for i in range(10):
if i >1:
print("http://www.budejie.com/" + str(i))
html = getHtml("http://www.budejie.com/" + str(i))
else:
html = getHtml("http://www.budejie.com/")
g(html)
下载成功
python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)的更多相关文章
- 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法
抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...
- Python抓取zabbix性能监控图
一.通过查询zabbix db的方式通过主机IP获取到所需要的graphid(比如CPU监控图.内存监控图等,每个图对应一个graphid),最后将图片保存到本地 注:该graph必须要在 scree ...
- nagios报错HTTP WARNING: HTTP/1.1 403 Forbidden解决方法
Nagios--localhost报警:"WARNING: HTTP/1.1 403 Forbidden "解决方法: In dashboard it shows alert on ...
- myeclipse关于svn更新报错:OPTIONS of '/svn/Xxx': 403 Forbidden
这个问题出现原因是其他人修改了我原本写作的代码位置,把两个类转移到了别的文件夹,我更新之后只显示除了他增加的文件夹而没有里面的类,同时爆出错误: 问题原因:svn版本号不匹配,即跳版本. 解决如下:r ...
- python抓取性感尤物美女图
由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...
- 使用Python抓取猫眼近10万条评论并分析
<一出好戏>讲述人性,使用Python抓取猫眼近10万条评论并分析,一起揭秘“这出好戏”到底如何? 黄渤首次导演的电影<一出好戏>自8月10日在全国上映,至今已有10天,其主演 ...
- python抓取知乎热榜
知乎热榜讨论话题,https://www.zhihu.com/hot,本文用python抓取下来分析 #!/usr/bin/python # -*- coding: UTF-8 -*- from ur ...
- Python 抓取网页并提取信息(程序详解)
最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...
- 使用 Python 抓取欧洲足球联赛数据
Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤 数据的采集和获取 数据的清洗,抽取,变形和装载 数据的分析,探索和预测 ...
随机推荐
- Async programming
Asynchrony, in computer programming, refers to the occurrence of events independent of the mainprogr ...
- MongoDB 中文的全文索引
MongoDB 从3.2 版本以后添加了对中文索引的支持: 官网链接:https://docs.mongodb.com/manual/reference/text-search-languages/ ...
- openjudge1.2
目录 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.2.10 1.2.1 描述 分别定义int,short类型的变量各一个,并依次输出 ...
- FPGA综合的约束
近日发现,有些逻辑电路的综合时间约束和布局布线约束相差太大时,难以布通.此时,应该选择尽量接近的时钟约束.
- TCP/IP协议族基本知识
常见的网络拓扑 两台主机通信的过程:应用进程产生消息,经由主机的 TCP/IP 协议栈发送到局域网(LAN),最后经过广域网(目前最大的广域网的因特网)中的网络设备(路由器)传给目的主机所在的局域网( ...
- shell 给文件每一行都添加指定字符串
[admin@localhost file]$ cat file hello hello hello hello hello [admin@localhost file]$ cat test.sh # ...
- player: 初始化分析
//1. //cocos 程序开始运行时执行的函数 bool AppDelegate::applicationDidFinishLaunching() { // initialize director ...
- PowerDesigner应用01 逆向工程之配置数据源并导出PDM文件
物理数据模型(Physical Data Model)PDM,提供了系统初始设计所需要的基础元素,以及相关元素之间的关系:数据库的物理设计阶段必须在此基础上进行详细的后台设计,包括数据库的存储过程.操 ...
- docker安装 与 基本配置
1.安装docker #yum remove docker \ docker-common \ container-selinux \ docker-selinux \ docker-engine \ ...
- WordPress入门 之 设置导航菜单
WordPress 3.0 添加了一个自定义导航菜单的功能,让你可以很自由地设置网站的导航菜单.现在大多数的主题也都支持这个功能了,那么,究竟该如何设置WordPress导航菜单?今天倡萌就介绍一下. ...