向大神学习,我抄一遍!!# -*- coding: utf-8 -*-
# @Time : 2019-05-08 02:47
# @Author : Ein
# @File : 花瓣网.py
# @Software: PyCharm
import requests
import json
import time
class RequestsHuaban:
def __init__(self):
self.url = 'https://huaban.com/boards/481662/?jvetal98&max={pin_id}&limit=100&wfl=1'
self.session = requests.Session()
self.path = r'/Users/sstubborn/Desktop/jpg'+'/' #设置你的下载路径
def callback(self,function,*parameter):
function(*parameter)
def run(self,pin):
url = self.url.format(pin_id=pin)
text= self.session.get(url=url).text
time.sleep(5)
json_data = text.split('["board"] = ')[1].split(';\napp._csr',)[0]
data = json.loads(json_data)
pinsList = data['pins']
item = {}
for pin_id in pinsList:
item['pin_id'] = pin_id['pin_id']
item['url'] = 'https://hbimg.huabanimg.com/' + pin_id['file']['key'] + '_fw658'
self.callback(self.download,item['url'],item['pin_id'])
self.callback(self.run, item['pin_id'])
def download(self,url,id):
response = self.session.get(url=url).content
path = self.path + str(id) + '.jpg'
with open(path,'wb') as fp:
fp.write(response)
if __name__ == '__main__':
test = RequestsHuaban()
test.run(pin='2363573713')
|