Python 爬取多玩图库

拿多玩图库练手,爬取了其图片,gif和视频
找到接口就好办多了,直接上代码

import urllib.request
import os
import json
import requests
import time
import re

#今日囧图的编号,gif编号,视频编号
jinristr = "138622" 
#生成文件时要对文件名字做处理
def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title
#接口
url = "http://tu.duowan.com/index.php?r=show/getByGallery&gid="
#生成有参数的接口
url = url + jinristr + "&_=" + str(int(round(time.time() * 1000)))
resp=requests.get(url)
dataJson=json.loads(resp.content)
listdaa=dataJson['picInfo']
listdaa.sort(key = lambda x:(int)(x['ding']))
str = ''
#循环找到的数据
for i in listdaa:
    print(i['ding'])
    print(i['add_intro'])
    str = str+i['add_intro']+"\n"
#在电脑中定义存放图片的位置并新建
path1='G:\\多玩爬取图片\\'+dataJson['gallery_title']
if os.path.exists(path1):
    pass
else:
    os.makedirs(path1)
#定义文件的连接
for i in listdaa:
	#后面的文件名应该是随着类型的不同而变化的
    picpath = path1+'\\'+validateTitle(i['add_intro']+'.gif')
    if not os.path.exists(picpath):
    	#下载
        urllib.request.urlretrieve(i['source'], picpath)
#写一个文本文件,里面放着他们的文件名字,这个其实没用的
txtpath = path1 + '\\1.txt'
f = open(txtpath,'w')
f.write(str)
f.close()

爬下来就这样子
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赫凯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值