如果需要專門出一篇文章,那就請多多評論回復
你的點贊是對我的無盡支持
程式含金量雖然不高,但是也有點,大佬勿噴!
程式中的保存檔案及輸入鏈接未經處理,可以根據自己需求對代碼進行修改(比如加個input什么的),還請認真看注釋!!!
#作者CSDN:https://blog.csdn.net/qq_45429426?spm=1011.2124.3001.5343 漫游感知
#請勿轉載!!!!
#沒有的庫請用pip指令下載
import requests #第三方庫,來發送請求等操作
import jsonpath #第三方庫,來決議json資料
from lxml import etree #第三方庫,來使用xpath決議網頁
import re
def lancode(url):
url = str(url)
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': 'lzt666.lanzous.com',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
respeno = requests.get(url=url,headers=headers).text
Html_lan_xpath = etree.HTML(respeno)
two_url_yuan = Html_lan_xpath.xpath('/html/body/div[3]/div[2]/div[4]/iframe/@src')[0]
two_url_replace = str(two_url_yuan).replace('/fn?','')
two_list = [two_url_replace,two_url_yuan] #一個不帶/fu?為請求頭做鋪墊,另一個則為請求表單做鋪墊
return two_list
def get_download_url(url_list):
page_url = 'https://lzt666.lanzous.com'+url_list[1]
page_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': 'codelen=1; pc_ad1=1',
'Host': 'lzt666.lanzous.com',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
data = {
'':url_list[0]
}
page_code_sign = requests.get(url=page_url,headers=page_headers,data=data).text
# print(page_code_sign)
txt = re.findall("'sign':(.*?),'ves'",page_code_sign)
txt_re = str(txt[1]).replace("'",'') #獲取正確傳參code
url_post = 'https://lzt666.lanzous.com/ajaxm.php'
headers = {
'Accept': 'application/json, text/javascript, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Length': '136',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'codelen=1; pc_ad1=1',
'Host': 'lzt666.lanzous.com',
'Origin': 'https://lzt666.lanzous.com',
'Referer': 'https://lzt666.lanzous.com' + url_list[1],
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
data = {
'action': 'downprocess',
'signs': '?ctdf',
'sign': txt_re,
'ves': '1',
'websign': None
}
post_url_download = requests.post(url=url_post,headers=headers,data=data).json()
# print(post_url_download)
url_file = jsonpath.jsonpath(post_url_download,'$..url')[0]
# print('Referer: https://lzt666.lanzous.com' + url_list[1],'sign:'+url_list[0])
download_file_url = 'https://vip.d0.baidupan.com/file/' + url_file
return str(download_file_url) #回傳檔案下載鏈接
def download(url):
'''
:param url: 需要下載的鏈接(前面函式已經決議好的)
:return:
'''
url = str(url)
data = {
'': url.rsplit('/')[-1].replace('?','').replace('=',':')
}
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'cookie': 'down_ip=1',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
file = requests.get(url=url,headers=headers,data=data).content
with open('./1.zip','wb') as file_down:
#這里測驗時下載的壓縮檔案,所以保存就這么寫了,用的時候可以根據自己的需求來改動
file_down.write(file)
file_down.close()
print('完成下載')
return True
a = lancode('https://lzt666.lanzous.com/xxxx') #在這里放入鏈接
b = get_download_url(a)
download(b)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258961.html
標籤:python
下一篇:Python創作新年祝福語
