本文仅供学习
用到的模块是
import requests
from lxml import etree
import os
最近发现有的影视网页是通过一个播放器来解析m3u8地址进行播放的,本想获取m3u8地址通过那个网页播放器来进行播放但有些复杂还不如直接获取网页代码来下载本地播放,以下是下载效果.
打开后也可以正常播放,因为他的播放器是用
这个播放器来完成的所以将网页代码获取下载到本地也是可以播放的,不比获取解析m3u8方便吗.
以下是全部代码.
import requests
from lxml import etree
import os
def jieX(url ,hXpath):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
r =requests.get(url ,headers =headers)
r.encoding ="unt-8"
html =etree.HTML(r.text)
src =html.xpath(hXpath)
return src
def diZhi(url ,src ,start):
ji =1
print(f"共有{len(src)}")
for i in src:
urll =url+i
html_list =src
r =requests.get(urll)
r.encoding ='utf-8'
if ji ==1:
filePath =str(i)
filePath =filePath[start[0]:start[1]]
newPath(filePath)
ji =0
fileName =str(i)
fileName =fileName[start[2]:]
rtext =r.text
downLoad(filePath ,fileName ,rtext)
return html_list
def downLoad(filePath ,fileName ,rtext):
with open(os.path.join(f"{filePath}",f"{fileName}"),"a",encoding="utf-8") as f:
f.write(rtext)
def newPath(filePath):
if os.path.exists(filePath):
print("文件夹已存在")
else:
os.makedirs(f"{filePath}" ,exist_ok=True)
print("已经创建文件夹")
##def download(filePath ,fileName ,rtext):
if __name__ =="__main__":
url ="https://ys2046.us/"
hXpath ="/html/body/div[3]/div/div[2]/ul//@href"
src =jieX(url ,hXpath)
start =[1 ,7 ,8]
html_list =[]
html_list =diZhi(url ,src ,start)
start =[1 ,5 ,6]
for i in html_list:
i =url+str(i)
hXpath ="/html/body/div[3]/div/div[2]/div/div[1]/div[2]/div[1]/ul[2]//@href"
src =jieX(i ,hXpath)
print(f"共{len(src)}集")
diZhi(url ,src ,start)
print("一部下载完成")
首先输入URL地址来requests.get() ,然后我xpath的是
通过右击检查查看
随后复制xpath地址通过 xpath(" ")来获取href ,随后进行循环将地址拼在一起,之后requests.get()解析随后用with open来进行下载r.text
start的功能是获取这个html文件的位置和名称这样在点击下一集的时候可以在本地跳转过去
def jieX(url ,hXpath): headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE' } r =requests.get(url ,headers =headers) r.encoding ="unt-8" html =etree.HTML(r.text) src =html.xpath(hXpath) return src def diZhi(url ,src ,start): ji =1 print(f"共有{len(src)}") for i in src: urll =url+i html_list =src r =requests.get(urll) r.encoding ='utf-8' if ji ==1: filePath =str(i) filePath =filePath[start[0]:start[1]] newPath(filePath) ji =0 fileName =str(i) fileName =fileName[start[2]:] rtext =r.text downLoad(filePath ,fileName ,rtext) return html_list def downLoad(filePath ,fileName ,rtext): with open(os.path.join(f"{filePath}",f"{fileName}"),"a",encoding="utf-8") as f: f.write(rtext)
将所有电视首页下载完后还要完成每一集代码的下载.
将每一部电视的网址保存在了html_list里面随后return返回html_list
然后通过循环重新使用diZhi函数进行下载
html_list =diZhi(url ,src ,start)
start =[1 ,5 ,6]
for i in html_list:
i =url+str(i)
hXpath ="/html/body/div[3]/div/div[2]/div/div[1]/div[2]/div[1]/ul[2]//@href"
src =jieX(i ,hXpath)
print(f"共{len(src)}集")
diZhi(url ,src ,start)
print("一部下载完成")
要将文件夹名和html文件名弄正确不然看的时候调转不了集数
大概流程就是这样具体的可以复制代码研究一下,我也是Python新手有些地方写的不好望包涵