Veer网站下载图片

运行环境

Python:python 3.7.0
IDE:PyCharm 2018.2.0
抓包工具:Fiddler 5.0
操作系统:Windows 7 (32 bit)
浏览器:Chrome

import random
import re

import requests
import urllib3

urllib3.disable_warnings()


class Veer:
    def __init__(self):
        self.s = requests.Session()
        self.s.verify = False
        self.s.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
        }
        self.phrase = ''
        self.uid = ''
        self.image_res_id = []
        self.download_url = []
        self.key = self.get_key()

    def get_key(self):
        s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
        return ''.join(random.choices(s, k=6))

    def get_captchKey(self):
        html = self.s.get('https://www.veer.com/sign-in').text
        return re.compile('"captchKey":"(.*?)"').findall(html)[0]

    def login(self, username, password):
        url = 'https://www.veer.com/ajax/passport/sign-in'
        js_data = {
            'account': username,
            'pageMedia': '',
            'pageType': '8',
            'password': password,
            'platform': '1',
            'stToken': '',
            't': self.get_captchKey()
        }
        r = self.s.post(url, json=js_data).json()
        if r['data']['errorType'] == 200:
            print('登录成功!')
            self.uid = r['data']['aUser']['uid']
            # 'Cookie': f'st={r["data"]["st"]}; uid={self.uid};
            # ticket={r["data"]["ticket"]}; name={""};
            # mobile={"手机号类似123****1234"}; payStatus={"0"}'
            # 起决定性作用的是 st
            self.s.headers.update(
                {
                    'Cookie': f'st={r["data"]["st"]}',
                }
            )
        else:
            print(r['data']['errorMessage'])
            exit()

    def get_image_res_id(self):
        self.phrase = input('请输入要下载的内容:')
        url = 'https://www.veer.com/ajax/search'
        js_data = {
            'changeTitle': '搜索结果 - Veer图库_全球领先的正版商业图片素材交易平台',
            'graphicalStyle': '',
            'key': self.key,
            'page': '1',
            'page_type': '6',
            'perpage': '100',
            'phrase': self.phrase,
        }
        r = self.s.post(url, json=js_data).json()
        id_resid = r['data']['list']
        for info in id_resid:
            self.image_res_id.append((info.get('id'), info.get('resId')))

    def get_download_url(self, img_id, res_id):
        url = 'https://www.veer.com/ajax/resDownload/download'
        js_data = {
            'authType': '3',
            'imageId': str(img_id),
            'key': self.key,
            'pageType': '6',
            'phrase': self.phrase,
            'resId': res_id,
            'uid': self.uid,
        }
        r = self.s.post(url, json=js_data).json()
        print(r)
        self.download_url.append(r['data']['downloadurl'])

    def download(self):
        # 保存图片到本地
        i = 1
        for download_url in self.download_url:
            print(f'正在下载第{i}张图片')
            img = requests.get(download_url).content
            with open(f'./{i}.jpg', 'wb') as f:
                f.write(img)
            i += 1


if __name__ == '__main__':
    v = Veer()
    v.login('手机号', '密码')
    v.get_image_res_id()
    for image_res_id in v.image_res_id[:5]:
        v.get_download_url(*image_res_id)
    # print(v.download_url)
    v.download()

你可能感兴趣的:(Veer网站下载图片)