python爬虫(五)图片下载爬虫

来源:互联网 发布:监控无网络视频怎么办 编辑:程序博客网 时间:2024/05/21 09:09

在网页的源代码中,图片的信息在<img> 标签中 其中<img src=>中表明图片所在地址,<img class=>表明图片类型,如表情图片,正常图片,还是广告图片


#--*coding:utf-8import urllibimport re  #正则表达式def get_content(url):"""获得文档"""html = urllib.urlopen(url)content = html.read()html.close()return contentdef get_images(info):"""<img class ="BDE_Image" src="http://" pic_ext="jpeg" changedsize="true" width="50" height="373">"""regex = r'class="BDE_Image" src="(.+?\.jpg)"'pat = re.compile(regex)i = 0images_code = re.findall(pat,info)#print len(images_code)for image_url in images_code:print image_urlurllib.urlretrieve(image_url,'%s.jpg'%i)i +=1url = "https://tieba.baidu.com/p/2772656630"info = get_content(url)print get_images(info)


原创粉丝点击