爬虫学习--下载图片
1.主要用到了urllib和re库
2.利用urllib.urlopen()函数获得页面源代码
3.利用正则匹配图片类型,当然正则越准确,下载的越多
4.利用urllib.urlretrieve()下载图片,并且可以重新命名,利用%S
5.应该是运营商有所限制,所以未能下载全部的图片,不过还是OK的
URL分析:
源码:
#coding=utf-8
import re
import urllib
def getHtml(url):
page=urllib.urlopen(url)
html=page.read();
return html
def getImage(html):
reg=r'src="(.*?\.jpg)" size'
imgre=re.compile(reg)
imgeList =re.findall(imgre,html)
x=0
for image in imgeList:
urllib.urlretrieve(image,'%s_hhh.jpg' % x)
x+=1
html=getHtml("https://tieba.baidu.com/p/5256641773")
getImage(html)