python利用urllib实现爬取京东网站商品图片的爬虫实例
作者:jingxian 时间:2022-05-01 12:32:56
本例程使用urlib实现的,基于python2.7版本,采用beautifulsoup进行网页分析,没有第三方库的应该安装上之后才能运行,我用的IDE是pycharm,闲话少说,直接上代码!
# -*- coding: utf-8 -*
import re
import os
import urllib
import urllib2
from bs4 import BeautifulSoup
def craw(url,page):
html1=urllib2.urlopen(url).read()
html1=str(html1)
soup=BeautifulSoup(html1,'lxml')
imagelist=soup.select('#J_goodsList > ul > li > div > div.p-img > a > img')
namelist=soup.select('#J_goodsList > ul > li > div > div.p-name > a > em')
#pricelist=soup.select('#plist > ul > li > div > div.p-price > strong')
#print pricelist
path = "E:/{}/".format(str(goods))
if not os.path.exists(path):
os.mkdir(path)
for (imageurl,name) in zip(imagelist,namelist):
name=name.get_text()
imagename=path + name +".jpg"
imgurl="http:"+str(imageurl.get('data-lazy-img'))
if imgurl == 'http:None':
imgurl = "http:" + str(imageurl.get('src'))
try:
urllib.urlretrieve(imgurl,filename=imagename)
except:
continue
'''
#J_goodsList > ul > li:nth-child(1) > div > div.p-img > a > img
#plist > ul > li:nth-child(1) > div > div.p-name.p-name-type3 > a > em
#plist > ul > li:nth-child(1) > div > div.p-price > strong:nth-child(1) > i
'''
if __name__ == "__main__":
goods=raw_input('please input the goos you want:')
pages=input('please input the pages you want:')
count =0.0
for i in range(1,int(pages+1),2):
url="https://search.jd.com/Search?keyword={}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&suggest=1.def.0.T06&wq=diann&page={}".format(str(goods),str(i))
craw(url,i)
count += 1
print 'work completed {:.2f}%'.format(count/int(pages)*100)
图片的命名为商品的名称,京东商品图片地址的属性很可能会有所变动,所以大家进行编写的时候应该举一反三,灵活运用!
这是我下载下来的手机类图片文件的截图:
我本地的爬取的速度很快,不到一分钟就能爬取100页上千个商品的图片!
来源:http://www.cnblogs.com/kfpa/p/7418843.html
标签:京东,商品,爬取,python,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
XML入门精解之文件格式定义
2008-02-25 13:57:00
用Mysql查询语句记录
2011-02-16 12:29:00
Select 控件Combox加强版(IE Only)
2008-07-23 13:26:00
php SQL防注入代码集合
2023-11-18 03:58:34
Pytorch中DataLoader的使用方法详解
2023-07-19 04:45:39
![](https://img.aspxhome.com/file/2023/4/61454_0s.png)
常用照片尺寸
2009-03-06 12:56:00
ASP 使用Filter函数来检索数组
2011-04-30 16:49:00
python制作小说爬虫实录
2023-09-30 17:39:25
httpwatch 的页面元素加载时间表
2008-02-13 08:28:00
![](https://img.aspxhome.com/file/UploadPic/20082/13/200821383430675s.gif)
oracle 触发器 学习笔记
2009-05-24 19:57:00
goland中使用leetcode插件实现
2023-07-23 10:43:09
Sql Server表死锁的解决方法分享
2011-09-01 19:08:00
了解WEB页面工具语言XML(四)应用分类
2008-09-05 17:18:00
JS中实现JAVA的hashCode算法
2008-08-03 17:00:00
python 计算一个字符串中所有数字的和实例
2022-07-19 01:42:33
几个javascript显示加载进度条代码
2008-01-19 10:52:00
初学ASP编程易犯的一个错误要注意
2008-11-07 15:08:00
五大提高ASP运行效率的技巧
2007-09-20 13:15:00
sql server 锁表语句分享
2012-02-12 15:49:20
Python办公自动化之Excel(中)
2023-03-07 05:48:15