先上学习链接,先学习
代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import re
import time
import json
import os
#用于去除掉创建文件是出现的特殊字符,下文中会使用到
def strip(path):
path = re.sub(r'[?\\*\“<>:/]','',str(path))
return path
#爬虫类
class Spider:
def __init__(self):
self.session = requests.session() #保持参数
#返回图片id列表
def gei_img_item_ids(self,start_url):
response = self.download(start_url)
if response:
html = response.text.encode('GBK','ignore').decode('GBk') #获取网页源代码
ids = re.findall(r'http://tu.duowan.com/gallery/(\d+).html',html) #获取图集的url
return set(ids)
#根据套图id获取套图信息
def gei_img_item_info(self,img_id):
#观察http://tu.duowan.com/index.php?r=show/getByGallery/&gid=136705&_=1522715109098使用正则
#136705:图片id 1522715109098:时间戳 *1000扩大位数
img_item_url ="http://tu.duowan.com/index.php?r=show/getByGallery/&gid=%s&_=%s" % (img_id,int(time.time()*1000))
response = self.download(img_item_url)
if response:
#返回json文件,内容多加观察,下文会使用到
return json.loads(response.text.encode('GBK','ignore').decode('GBk'))
#下载器
def download(self,url):
try:
return self.session.get(url)
except Exception as e:
print(e)
#print(response.text.encode('GBK','ignore').decode('GBk'))
#根据套图的信息,持久化
def save_img(self,img_item_info):
#设置文件名
dir_name = strip(img_item_info['gallery_title'].strip())
#print(dir_name)
#判断文件是否存在,没有就创建一个
if not os.path.exists(dir_name):
os.makedirs(dir_name)
#根据json文件中内容,取出信息,图片后缀名不同,所以要截取后缀名
for img_info in img_item_info['picInfo']:
img_name = strip(img_info['title'].strip())
img_url = img_info['url']
pix = (img_url.split('/')[-1]).split('.')[-1]
#合成图片的全路径
img_path = os.path.join(dir_name,"%s.%s" % (img_name,pix))
#判断路径是否存在
if not os.path.exists(img_path):
response = self.download(img_url)
if response:
img_data =response.content
#保存
with open(img_path,'wb') as f:
f.write(img_data)
#运行
def run(self,start_url):
img_ids = self.gei_img_item_ids(start_url)
# print(img_ids)
for img_id in img_ids:
img_item_info =self.gei_img_item_info(img_id)
self.save_img(img_item_info)
if __name__ == '__main__':
spider = Spider() #实例化对象
start_url = 'http://tu.duowan.com/m/meinv' #起始url
spider.run(start_url) #运行
gei_img_item_info中json文件的内容
{'gallery_title': '这波怎么说?请宠爱这只粘人的小狐狸', 'picInfo': [{'title': '这波怎么说?请宠爱这只粘人的小狐狸 (1)', 'pic_id': '2426608', 'ding': '39', 'cai': '6', 'old': '30', 'cover_url': 'http://s1.dwstatic.com/group1/M00/6A/02/6f17a51ab1a55f7832fd022daaa5239d.jpg', 'file_url': None, 'file_width': '650', 'file_height': '433', 'url': 'http://s1.dwstatic.com/group1/M00/6A/02/97b00784891c207959d9911076961719.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/6A/02/97b00784891c207959d9911076961719.jpg', 'mp4_url': '', 'sort': '0', 'cmt_md5': 'e60869c692617cc587c95f9001b3013a', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426608', 'add_intro': 'CN: 笙歌23333'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (2)', 'pic_id': '2426609', 'ding': '15', 'cai': '1', 'old': '2', 'cover_url': 'http://s1.dwstatic.com/group1/M00/99/91/7f3c5d35cce3efaf2c298b2e8ef54425.jpg', 'file_url': None, 'file_width': '650', 'file_height': '433', 'url': 'http://s1.dwstatic.com/group1/M00/99/91/54db81a62174e9d72543d529be9e025d.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/99/91/54db81a62174e9d72543d529be9e025d.jpg', 'mp4_url': '', 'sort': '1', 'cmt_md5': 'e11fe988bbedd6b99624add4ce20d807', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426609', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (2)'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (3)', 'pic_id': '2426610', 'ding': '11', 'cai': '0', 'old': '0', 'cover_url': 'http://s1.dwstatic.com/group1/M00/BD/10/426a5abb0f210b948a4cd03292ac756e.jpg', 'file_url': None, 'file_width': '650', 'file_height': '488', 'url': 'http://s1.dwstatic.com/group1/M00/BD/10/b5a5fcc8c8751736c8277c0a9cfc852f.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/BD/10/b5a5fcc8c8751736c8277c0a9cfc852f.jpg', 'mp4_url': '', 'sort': '2', 'cmt_md5': '9930397f015a9d3ca3d85eba5010023b', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426610', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (3)'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (4)', 'pic_id': '2426611', 'ding': '9', 'cai': '1', 'old': '0', 'cover_url': 'http://s1.dwstatic.com/group1/M00/77/A9/1925b2927590eed12e5fdfd190d55d71.jpg', 'file_url': None, 'file_width': '650', 'file_height': '477', 'url': 'http://s1.dwstatic.com/group1/M00/77/A9/5ee6d78393fc109bf4a11e50525d1996.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/77/A9/5ee6d78393fc109bf4a11e50525d1996.jpg', 'mp4_url': '', 'sort': '3', 'cmt_md5': 'f31336881a0ed898dae482001b335c32', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426611', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (4)'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (5)', 'pic_id': '2426612', 'ding': '9', 'cai': '0', 'old': '0', 'cover_url': 'http://s1.dwstatic.com/group1/M00/B7/BC/6d63c7c6d282343dfcbfabcb3c242f72.jpg', 'file_url': None, 'file_width': '650', 'file_height': '975', 'url': 'http://s1.dwstatic.com/group1/M00/B7/BC/fe11da941c74914f478c1213bfe7159e.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/B7/BC/fe11da941c74914f478c1213bfe7159e.jpg', 'mp4_url': '', 'sort': '4', 'cmt_md5': '88dce443fd2ccf759e96f58971af3398', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426612', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (5)'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (6)', 'pic_id': '2426613', 'ding': '5', 'cai': '0', 'old': '1', 'cover_url': 'http://s1.dwstatic.com/group1/M00/91/87/fdfbf99897262fae37b214a1692c8e04.jpg', 'file_url': None, 'file_width': '650', 'file_height': '803', 'url': 'http://s1.dwstatic.com/group1/M00/91/87/b63af952c4c58096294c13cce5062565.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/91/87/b63af952c4c58096294c13cce5062565.jpg', 'mp4_url': '', 'sort': '5', 'cmt_md5': '84ef6b4e3127e96d4b599e24bbac096c', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426613', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (6)'}, {'title': '这波怎么说?请宠爱这只粘人的小狐狸 (7)', 'pic_id': '2426614', 'ding': '11', 'cai': '1', 'old': '1', 'cover_url': 'http://s1.dwstatic.com/group1/M00/4B/27/bc3b5dcaf48ae1f0cf063db740a7c4e2.jpg', 'file_url': None, 'file_width': '650', 'file_height': '975', 'url': 'http://s1.dwstatic.com/group1/M00/4B/27/bf07b237c6416098ce78800d77835e57.jpg', 'source': 'http://s1.dwstatic.com/group1/M00/4B/27/bf07b237c6416098ce78800d77835e57.jpg', 'mp4_url': '', 'sort': '6', 'cmt_md5': 'c520eee9ef745c9aeb0001c481040455', 'cmt_url': 'http://tu.duowan.com/gallery/136418.html#2426614', 'add_intro': '这波怎么说?请宠爱这只粘人的小狐狸 (7)'}], 'hiidoId': ['_9e6801b']}
成果