美文网首页
python3获取网页上的图片

python3获取网页上的图片

作者: firststep | 来源:发表于2018-10-19 17:56 被阅读0次

    由于今天工作早早做完,就想从网上找一些偶像的照片等写自己的网站使用。然后又正在学习python就想着扒一些图片。但是发现有时候网页上出现中文就不行,后来还是解决了,棒棒的。

    import urllib.request
    import re
    from urllib.request import quote, unquote
    
    url = "https://baike.baidu.com/item/刘亦菲/136156?fr=aladdin" #http://findicons.com/pack/2787/beautiful_flat_icons
    # utf8编码,指定安全字符。
    url = quote(url, safe=";/?:@&=+$,", encoding="utf-8")
    webPage=urllib.request.urlopen(url) #https://baike.baidu.com/item/刘亦菲/136156?fr=aladdin
    #print (webPage)
    data = webPage.read()
    data = data.decode('UTF-8')
    #print (data)
    k = re.split(r'\s+',data)
    
    s = []
    sp = []
    si = []
    for i in k :
        if (re.match(r'src',i) or re.match(r'href',i)):
            if (not re.match(r'href="#"',i)):
                if (re.match(r'.*?png"',i) or re.match(r'.*?ico"',i) or re.match(r'.*?jpg"',i) ):
                    if (re.match(r'src',i)):
                        s.append(i)
    
    for it in s :
        if (re.match(r'.*?png"',it)):
            sp.append(it)
    
    for it in s :
        if (re.match(r'.*?jpg"',it)):
            sp.append(it)
    
    cnt = 0
    cou = 1
    for it in sp:
        m = re.search(r'src="(.*?)"',it)
        iturl = m.group(1)
        print(iturl)
        if (iturl[0]=='/'):
            continue;
        web = urllib.request.urlopen(iturl)
        itdata = web.read()
        if (cnt%3==1 and cnt>=4 and cou<=30):
            f = open('d:/image/'+str(cou)+'.png',"wb")
            cou = cou+1
            f.write(itdata)
            f.close()
            print(it)
        cnt = cnt+1
    

    如图所示:


    截图.png

    刚学python,有什么问题欢迎交流。

    相关文章

      网友评论

          本文标题:python3获取网页上的图片

          本文链接:https://www.haomeiwen.com/subject/pfduzftx.html