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在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤 数据的采集和获取 数据的清洗,抽取,变形和装载 数据的分析,探索和预测 ...
随机推荐
- Socket内核调用数SYSCALL_DEFINE3
http://blog.chinaunix.net/uid-20788636-id-4408261.html 前言: 对于Linux内核的Socket系列文章都是依据于:Linux-3.14.5的版本 ...
- Web安全之CSRF基本原理与实践
阅读目录 一:CSRF是什么?及它的作用? 二:CSRF 如何实现攻击 三:csrf 防范措施 回到顶部 一:CSRF是什么?及它的作用? CSRF(Cross-site Request Forger ...
- ZOJ3261-Connections in Galaxy War-(逆向并查集+离线处理)
题意: 1.有n个星球,每个星球有一个编号(1-n)和一个能量值. 2.一开始将某些星球连通. 3.开战后有很多个操作,查询某个星球能找谁求救或者摧毁两颗星球之间的连通路径,使其不能连通.如果连通则可 ...
- win10系统绑定本地IP和mac地址
第一步:找到自己的IP和mac地址 1.按着win键+R键,输入cmd(大小写都一样) 2.命令: ipconfig /all #查看所有地址 然后按“回车键” 3.这样 ...
- 8.学习springmvc的拦截器
一.springmvc拦截器介绍和环境搭建 1.介绍: 过滤器:servlet中的一部分,可以拦截所有想要访问的资源. 拦截器:SpringMVC框架中的,只能在SpringMVC中使用并且只能过滤控 ...
- 洛谷 P5614题解
吐槽:数据好像有点水,直接枚举到200可以得80 points. 另:我还是太弱了,比赛的时候只有90 points,#7死卡不过去,最后发现是没有判断 \(z_1\) 和 \(z_2\) 的范围-- ...
- Hash总结
算法介绍 这个没什么好说的,就是一段比较简单的代码,具体的话要看题目. 自然溢出 这个是比较好用而且容易被卡的一种Hash方式. 用\(2^{64}\)当模数然后做\(Hash\),常数比较小. 单模 ...
- 如何设置select只读不可编辑且select的值可传递(摘自百度)
selectname="role"id="role"οnfοcus="this.defaultIndex=this.selectedIndex;&qu ...
- 第01组 Alpha冲刺(2/6)
队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11861798.html 作业博客: https://edu.cnblogs.com/campus/fz ...
- Kubernetes中如何让Deployment更新镜像
问题描述 我的deployment有单个pod,我的自定义docker镜像如下: 123 containers: - name: mycontainer image: myimage:latest 在 ...