qiujw 发表于 2022-8-10 12:15

【原创源码】【Python】获取某云网盘下载直链

本帖最后由 qiujw 于 2022-8-17 15:22 编辑

# -*- coding: utf-8 -*-
import time
import requests
requests.packages.urllib3.disable_warnings()
import re
import random

# 由于蓝奏云经常变更
# 下载连接的匹配规则发生变了,则需要修改以下正则表达式 var_domian_reg_arr 和 var_query_reg_arr
# 下载域名
var_domian_reg_arr = [
    # # r: 不转义,原始字符串
    # var pototo = 'https://develope.lanzoug.com/file/';
    r"var\s*pototo\s*=\s*'(.+?)'",
    # var cppat = 'https://develope.lanzoug.com/file/';
    r"var\s*cppat\s*=\s*'(.+?)'",
]
# 下载token
var_query_reg_arr = [
    # # r: 不转义,原始字符串
    # var spototo = '?xxxx';
    r"var\s*spototo\s*=\s*'(.+?)'",
    # submit.href =cppat + '?xxx'
    # r"submit.href\s*=\s*cppat\s*\+\s*'(.+?)'",
    r"submit.href\s*=\s*.*\s*\+?\s*'(.+?)'"
]
# 默认下载域名
default_domain = 'https://develope.lanzoug.com/file/'

# 请求超时
timeout = 50

# 蓝奏云Python真实地址解析
def get_lanzou_download_url(url, retryTimes=3):
    headers = {
      'origin': 'https://www.lanzous.com',
      'accept-language': 'zh-CN,zh;q=0.9',
      'X-Forwarded-For': '.'.join('%s' % random.randint(0, 255) for i in range(4)),
      'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36'
    }

    # 加上tp, 手机端
    if url.rfind('/tp/') == -1:
      ridx = url.rindex('/')
      url = url + '/tp' + url
      # print(url)

    # 请求下载页面
    resp = None
    try:
      resp = requests.get(url, headers=headers, timeout=timeout, verify=False)
    except Exception as err:
      print(err)

    if resp is None or not responseStatusOK(resp):
      retryTimes -= 1
      if retryTimes < 0:
            return None
      wait_some_time()
      return get_lanzou_download_url(url, retryTimes)

    resp.encoding = "utf-8"
    page_content = resp.text
    #print(page_content)
    if not page_content:
      print('Status: %u, Url: %s, content is empty' % (resp.status_code, resp.url))
      retryTimes -= 1
      if retryTimes < 0:
            return None
      wait_some_time()
      return get_lanzou_download_url(url, retryTimes)

    # 下载域名
    down_domain = get_reg_value_by_content(page_content, var_domian_reg_arr, default_domain)
    # 下载token
    download_query = get_reg_value_by_content(page_content, var_query_reg_arr)

    if down_domain and download_query:
      return down_domain + download_query
    else:
      print("Url: %s, down_domain:%s download_query: %s" %(resp.url, down_domain, download_query))
      return None

def get_reg_value_by_content(page_content, reg_arr, default_val=None):
    return_val = default_val
    for var_reg in reg_arr:
      match_val = re.search(var_reg, page_content)
      if match_val and match_val.group(1):
            return_val = match_val.group(1)
            break
    return return_val

def responseStatusOK(resp):
    if resp is None:
      print('resp is None, Url: %s' % resp.url)
      return False
    if resp.status_code != requests.codes.OK:
      print('Status: %u, Url: %s' % (resp.status_code, resp.url))
      return False
    return True

def wait_some_time(minMls=1000, maxMls=5000):
    sleep_seconds = random.randint(minMls, maxMls) / 1000
    time.sleep(sleep_seconds)
    return sleep_seconds

if __name__ == '__main__':
    print(get_lanzou_download_url('https://wwd.lanzouw.com/i269V065bmza'))

wty3025 发表于 2022-8-10 17:17

import requests
import re


def get_lanzou_download_url(url):
    headers = {
      'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36'
    }
    response = requests.get(url, headers=headers)

    sign_url = re.compile(r'ifr2.*?ifr2.*?src="(?P<sign_url>.*?)"', re.S).search(response.text).group('sign_url')
    sign_response = requests.get('https://wwd.lanzouw.com/' + sign_url)
    Query = re.compile(r"ajaxdata = '(?P<signs>.*?)';.*?= '(?P<websignkey>.*?)';.*?vsign = '(?P<sign>.*?)';", re.S).search(sign_response.text)
    data = {
      'action': 'downprocess',
      'signs': Query.group('signs'),
      'sign': Query.group('sign'),
      'ves': '1',
      'websignkey': Query.group('websignkey')
    }

    download_url = 'https://wwd.lanzouw.com/ajaxm.php'
    headers['referer'] = 'https://wwd.lanzouw.com/' + sign_url
    download_response = requests.post(download_url, headers=headers, data=data)
    print('https://develope.lanzoug.com/file/' + download_response.json()['url'])


if __name__ == '__main__':
    get_lanzou_download_url('https://wwd.lanzouw.com/i269V065bmza')

wty3025 发表于 2022-8-10 16:37

你写的什么乱七八糟的,至于那么麻烦吗

jjyfzx 发表于 2022-8-10 12:55

沙发   ,,,,,,,                  

iawyxkdn8 发表于 2022-8-10 13:04


坐在沙发上   ,,,,,,,         

路人王2021 发表于 2022-8-10 13:09

感谢分享下载了

qqdns 发表于 2022-8-10 13:15

蓝蓝盘,感谢分享

DMSama 发表于 2022-8-10 13:16

谢谢分享!!!{:1_899:}

aiyao 发表于 2022-8-10 13:40

已收藏,感谢大哥

Britney 发表于 2022-8-10 13:45

你好,某度网盘有吗

icodeme 发表于 2022-8-10 13:48

这个要怎么使用

wkdxz 发表于 2022-8-10 14:09

这个好,省得用selenium了!!
页: [1] 2 3
查看完整版本: 【原创源码】【Python】获取某云网盘下载直链