【Python3 爬虫】14_爬取淘宝上的手机图片,,现在我们想要使用爬虫
【Python3 爬虫】14_爬取淘宝上的手机图片,,现在我们想要使用爬虫
现在我们想要使用爬虫爬取淘宝上的手机图片,那么该如何爬取呢?该做些什么准备工作呢?
首先,我们需要分析网页,先看看网页有哪些规律
打开淘宝网站http://www.taobao.com/
我们可以看到左侧是主题市场,将鼠标移动到【女装/男装/内衣】这一栏目,我们可以看到更细类的展示
假如我们现在需要爬取【羽绒服】,那么我们进入到【羽绒服】衣服这个界面
此时查看浏览器地址,我们可以看到
网址复制到word或者其他地方会发生url转码
我们可以选中【羽绒服模块的第1,2,3页进行网址对比】,对比结果如下:
从上图我们可以看出:三页的s值都是相差60
然后我们再看下图片地址:
图片中标记的地方或许是两个图片最大的差别,于是打开源代码搜索
图片1搜索结果
图片2搜索结果
从两个网址我们发现了共同的特征:都是以"pic_url":"//开头,网址分析到此结束,那么我们接下来就写代码了。
代码如下:
import urllib.requestimport re #设置关键字keywords = "羽绒服"#quote函数进行url编码(屏蔽特殊的字符)key = urllib.request.quote(keywords)#设置User-Agentheaders=("User_Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0")#自定义openeropener = urllib.request.build_opener()opener.addheaders = [headers]urllib.request.install_opener(opener)#循环遍历抓取for i in range(0,2): url = "https://s.taobao.com/list?spm=a21bo.2017.201867-links-0.3.5af911d9rLmo4K&q="+key+"&cat=16&style=grid&seller_type=taobao&bcoffset=12&s="+str(i*60) #print(url) content = urllib.request.urlopen(url).read().decode("utf-8","ignore") rule = ‘"pic_url":"//(.*?)"‘ #正则匹配 imglist = re.compile(rule).findall(content) #获取图片列表 for j in range(0,len(imglist)): img = imglist[j] imgurl = "http://"+img file = "D://source//img//"+str(i)+str(j)+".jpg" urllib.request.urlretrieve(imgurl,filename=file)
爬取完毕后,我们可以打开D:\source\img查看
我们已经成功爬取,并且爬取的图片与页面上是一致的。
【Python3 爬虫】14_爬取淘宝上的手机图片
相关内容
- 《笔记》python itertools的groupby分组数据处理,itertoolsg
- Python网络爬虫(四),python网络爬虫,关于Robots协议
- vscode Python Pylint(代码检测插件),vscodepylint, 暑假刚开始
- Python+Selenium 自动化实现实例-模块化调用,pythonseleniu
- python使用xlrd读取excel数据时,整数变小数的解决办法,
- Python 爬虫实例(7)—— 爬取 新浪军事新闻,python新浪
- python 输出所有大小写字母, range()以及列表切片,,所以
- Python——graphviz及pydotplus安装步骤,graphvizpydotplus,Pyth
- python numpy中sum()时出现负值,pythonnumpy,import num
- Python+pandas+matplotlib数据分析与可视化案例,pandasmatplo
评论关闭