首页 > 编程语言 > 详细

Python学习---网页爬虫[下载图片]

时间:2018-07-29 11:24:38      阅读:207      评论:0      收藏:0      [点我收藏+]

爬虫学习--下载图片

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)

Python学习---网页爬虫[下载图片]

原文:https://www.cnblogs.com/ftl1012/p/9384457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!