Python 爬取图虫网图片

参考网址:https://www.cnblogs.com/baijifeilong/p/3708198.html

爬取图虫网上关于墨镜的照片

图虫网搜索墨镜之后的网址

https://stock.tuchong.com/search?source=tc_pc_home_search&term=“墨镜”

然后通过urllib 获取页面html的文本,对文本进行分析 发现文本中只有图片的ID


从网页中打开图片链接 发现链接规律为


由此 发现图片的URL可以使用

urllib.request.urlretrieve 获取图片  

代码如下:

import re, os, time
import urllib.parse
import urllib.request


def getHtml(url):#取得网页的html纯文本
    return urllib.request.urlopen(url).read().decode('utf-8')


if __name__ == '__main__':
    print('---图虫图片抓取器---')
    for n in range(1, 51):
        pageNo = n
        print(pageNo)
        folder = savepath+str(pageNo)
        if not os.path.exists(folder):
            os.makedirs(folder)
    #url汉字编码处理
        url = 'https://stock.tuchong.com/search?source=tc_pc_home_search&term={}&page={}'.format(urllib.parse.quote('墨镜'), str(pageNo))
    #获取页面HTML文本
        html = getHtml(url)
        x = 0
        pattern = re.compile(r'\d+')
        match = pattern.findall(html)
        num = len(match)
        for i in range(num):
            if len(match[i])>15:
                print(match[i])
                imageurl = "http://p1.pstatp.com/weili/m/"+match[i]+".webp"
                urllib.request.urlretrieve(imageurl,folder+'\\%s.jpg'%x)
                x+=1
        print("第 "+str(pageNo)+" 下载结束")

    print('下载结束。')

猜你喜欢

转载自blog.csdn.net/ywj541726330/article/details/80542366