Python爬取某拍短視頻
一、抓取目標(biāo)
目標(biāo)網(wǎng)址:美拍視頻

二、工具使用
開發(fā)環(huán)境:win10、python3.7
開發(fā)工具:pycharm、Chrome
工具包:requests、xpath、base64
三、重點學(xué)習(xí)內(nèi)容
爬蟲采集數(shù)據(jù)的解析過程
js代碼調(diào)試技巧
js逆向解析代碼
Python代碼的轉(zhuǎn)換
四、項目思路解析
進(jìn)入到網(wǎng)站的首頁
挑選你感興趣的分類
根據(jù)首頁地址獲取到進(jìn)入詳情頁面的超鏈接的跳轉(zhuǎn)地址

找到對應(yīng)加密的視頻播放地址數(shù)據(jù)

這個數(shù)據(jù)是靜態(tài)的網(wǎng)頁數(shù)據(jù),通過js代碼進(jìn)行解碼的
找到對應(yīng)的解析代碼
先找到視頻的播放地址
找到解析視頻地址的加密js文件
點擊播放的時候會觸發(fā)文件

大致能看出來這個是base64加密之后的數(shù)據(jù)
在對應(yīng)的js文件里搜索關(guān)鍵字
找到j(luò)s的加密方式

js函數(shù)的一些函數(shù)的用法
# eplace()方法用于在字符串中用一些字符替換另一些字符 # parseInt 數(shù)據(jù)轉(zhuǎn)換成對應(yīng)的整型 # base64.atob對base64編碼過的字符串進(jìn)行解碼 # substring 方法可在字符串中抽取從 start 下標(biāo)開始的指定數(shù)目的字符

將js代碼轉(zhuǎn)換成Python代碼
import base64
def decode(data):
def getHex(a):
return {
'str': a[4:],
'hex': ''.join(list(a[:4])[::-1]),
}
def getDec(a):
b = str(int(a, 16))
return {
'pre': list(b[:2]),
'tail': list(b[2:]),
}
def substr(a, b):
c = a[0: int(b[0])]
d = a[int(b[0]): int(b[0]) + int(b[1])]
return c + a[int(b[0]):].replace(d, "")
def getPos(a, b):
b[0] = len(a) - int(b[0]) - int(b[1])
return b
b = getHex(data)
c = getDec(b['hex'])
d = substr(b['str'], c['pre'])
return base64.b64decode(substr(d, getPos(d, c['tail'])))
print(decode("e121Ly9tBrI84RdnZpZGVvMTAubWVpdHVkYXRhLmNvbS82MGJjZDcwNTE3NGZieXBueG5udnRwMTA5N19IMjY0XzFfNWY3YThmM2U0MTEwNy5tc2JVjAu3EDQ="))
得出最終視頻播放地址


五、簡易源碼分享
import requests
from lxml import etree
import base64
def decode_mp4(data):
def getHex(a):
return {
'str': a[4:],
'hex': ''.join(list(a[:4])[::-1]),
}
def getDec(a):
b = str(int(a, 16))
return {
'pre': list(b[:2]),
'tail': list(b[2:]),
}
def substr(a, b):
c = a[0: int(b[0])]
d = a[int(b[0]): int(b[0]) + int(b[1])]
return c + a[int(b[0]):].replace(d, "")
def getPos(a, b):
b[0] = len(a) - int(b[0]) - int(b[1])
return b
b = getHex(data)
c = getDec(b['hex'])
d = substr(b['str'], c['pre'])
return base64.b64decode(substr(d, getPos(d, c['tail'])))
# 運行主函數(shù)
def main():
url = 'https://www.meipai.com'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
response = requests.get(url=url, headers=headers)
html_data = etree.HTML(response.text)
href_list = html_data.xpath('//div/a/@href')
# print(href_list)
for href in href_list:
res = requests.get('https://www.meipai.com' + href, headers=headers)
html = etree.HTML(res.text)
name = html.xpath('//div[@id="detailVideo"]/img/@alt')[0]
mp4_data = html.xpath('//div[@id="detailVideo"]/@data-video')[0]
# print(name, mp4_data)
mp4_url = decode_mp4(mp4_data).decode('utf-8')
print(mp4_url)
result = requests.get("http:" + mp4_url)
with open(name + ".mp4", 'wb') as f:
f.write(result.content)
f.close()
if __name__ == '__main__':
main()
到此這篇關(guān)于Python爬取某拍短視頻的文章就介紹到這了,更多相關(guān)Python爬取視頻內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信