Python爬取堆糖网的表情包(再也不用担心斗图失败了)

  • 爬取堆糖网的一千张表情包
import requests
import urllib.request

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"}
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)

start_part='"path":"'
end_part='"'

pice_list=[] # 保存所有的表情包的URL
for x in range(0,1000,100):#1000张图片
    end = 0
    url = "https://www.duitang.com/napi/blog/list/by_search/?kw=%E8%A1%A8%E6%83%85%E5%8C%85&start={0}&limit=100".format(x)
    req=requests.get(url).content.decode()
    while req.find(start_part,end)!=-1:
        urllist=[]
        req=requests.get(url).content.decode()
        start=req.find(start_part,end)+len(start_part)
        end=req.find(end_part,start)
        urlnow=req[start:end]
        print(urlnow)
        urllist.append(urlnow)
        pice_list.extend(urllist)

# 将表情包写入硬盘中
for x in range(0,len(pice_list)):
    data="E:/表情包/"+str(x)+".jpg"
    req=requests.get(pice_list[x]).content
    with open(data,"wb") as  file:
        file.write(req)

你可能感兴趣的:(Python爬取堆糖网的表情包(再也不用担心斗图失败了))