爬取京东商城的手机图片
来源:互联网 发布:网络被攻击怎么办 编辑:程序博客网 时间:2024/04/29 10:18
按照韦玮老师书中代码,如下
import reimport urllib.requestdef craw(url, page): html = urllib.request.urlopen(url).read() html = str(html) pat1 = '<div id="plist".+? <div class="page clearfix">' result1 = re.compile(pat1).findall(html) result1 = result1[0] pat2 = '<img width="220" height="220" data-img="1" src="//(.+?\.jpg)">' imagelist = re.compile(pat2).findall(html) x = 1 for imageurl in imagelist: imagename = "D://spider/img1/" + str(page) + str(x) + ".jpg" imageurl = "http://" + imageurl try: urllib.request.urlretrieve(imageurl, filename=imagename) except urllib.error.URLError as e: if hasattr(e, "code"): print(e.code) x+=1 if hasattr(e, "reason"): print(e.reason) x+=1 x+=1for i in range(1, 79): url="https://list.jd.com/list.html?cat=9987653,655&page=" + str(i) craw(url, i)
代码思路:
1.通过urllib.request.urlopen(url).read()
读取对应网页的源代码
2.按照pat1进行第一次过滤
3.按照pat2进行第二次过滤,并将图片地址存到一个列表中
4.通过urllib.request.urlretrieve(imageurl, filename=imagename)
将图片保存到本地
几个问题:
1.两次过滤的效率会高过一次直接过滤吗?如果不是,是为了防止爬取到其他图片?
2.pat1中的.?+没有加(),而pat2中加了,是为什么。(已解决,加()会获取到()中的内容,不加()会获取包含()外的内容)
3.在处理URLError时
if hasattr(e, "code"):
print(e.code)
x+=1
if hasattr(e, "reason"):
print(e.reason)
x+=1
当错误为HTTPError时,两次判断都为true,那么是不是就会变成x+=2
,会漏掉一些图片
本人新手程序猿一枚,如果有大佬路过,还请多多指教
阅读全文
0 0
- 爬取京东商城的手机图片
- 首页商城18商城大的折扣手机
- 收集到的手机应用商城的地址
- 微信商城系统与手机APP的优势对比!
- 京东商城手机频道商品价格信息的抓取
- 仿照小米商城手机APP的html5页面
- 手机图片的获取
- 购物商城---图片服务器
- 仿天猫商城鼠标移近图片变暗的jquery特效代码
- 仿天猫商城鼠标移近图片变暗的jquery特效代码
- 淘淘商城系列(三)图片服务器的搭建
- 我的手机图片站
- 上传手机图片的问题
- 手机端图片的自适应--%
- 适合手机的图片轮播图
- 旋转手机拍照的图片
- android 扫描手机上的图片 获取手机图片路径
- scrapy框架爬取京东商城商品的评论
- nginx+vsftpd搭建图片服务器
- ubuntu16.04+NVIDIA GTX1050ti 显卡驱动PPA安装 +CUDA8.0
- mybatis逆向工程的创建和使用
- 不使用(a+b)/2这种方式,求两个数的平均值
- 标签
- 爬取京东商城的手机图片
- windows安装redis
- laravel学习笔记之查询构造器
- 【BZOJ3401】【单调栈】[Usaco2009 Mar]Look Up 仰望 题解
- 字符串拷贝函数和字符串比较函数C语言实现方法
- Android应用性能如何优化
- 人类\经济\太阳,管他呢
- Centos7安装JDK,Tomcat
- vim 打开sudo权限文件不退出保存