python爬图 准备多线程

#codeing = utf-8
#官方3.0版本已经把urllib2,urlparse等五个模块都并入了urllib中
import urllib.request
import re

def getHtml(url):
    #打开连接
    page = urllib.request.urlopen(url)
    #获取网页内容
    html = page.read()
    print(html)
    return html

def getImg(html):
    #正则表达式
    reg = r'src="(.+?\.jpg)" alt='
    imgre = re.compile(reg)
    #以列表的形式返回能匹配的子串
    imgList = re.findall(imgre,html.decode('utf-8'))
    x=0
    for imgurl in imgList:
        #把爬取到的资源保存到本地
        urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
        x+=1
    return imgList
#输入你想要爬取的网站
#url='https://www.113yq.com/pic/html28/index_3.html'
html=getHtml(new_url)
#html=getHtml("http://pic.yxdown.com/list/0_0_1.html")
print(getImg(html))

--------------------- 
作者:热心市民大G 
来源:CSDN 
原文:https://blog.csdn.net/tyt_xiaotao/article/details/80209398 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/xiaohe520/p/10821679.html