Python---requests以及正則獲取電影資源,PC端以及安卓端均可使用

俗話說的好,餓死偷懶兒的,撐死勤快的,自己動手,豐衣足食,告別廣告,告別無卡頓。
更多原創博客分享見博客主頁

程序說明

  1. 個人在使用,無論是PC端還是Android端,都不是像某訊視頻、愛某藝視頻客戶端一樣的界面,都並不美觀,運行出來的程序都只是控制檯黑窗口的形式,並且播放形式爲瀏覽器中播放。若介意,請直接跳過
  2. 只是分享一個思路,不確保可以永久使用,如有其他更改,會更新博客
  3. 可以GUI讓界面更加美觀,在文章結尾有展示,但是暫不提供
  4. PC端、Android端 均無廣告,清新明瞭,無需任何配置,打開即用

PC 端

所用工具

  1. Pycharm Community 2019.3.1
  2. requests、re

三大模塊

搜索

通過爬取ok資源採集獲取影片信息

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

import requests
import re
# 下載網頁源代碼,這個網站很大方,不用添加任何的請求頭,便可獲取信息,安逸
def download_url(video_name = None):
    search_url = 'http://www.okzy.co/index.php?m=vod-search'
    # Ajax請求,這是一個調用的接口,請求信息包括兩項 關鍵字即電影名稱、提交方式即固定的 search
    from_data = {
        'wd':video_name,
        'submit':'search'
    }
    
    # 發送請求,獲得網頁源代碼
    response = requests.get(url = search_url,params = from_data)
    response.encoding = 'utf-8'
    html = response.text
    return html

# 解析源代碼,對於這個網站,利用正則表達式非常簡單,獲得已有的電影名稱以及地址
def parse_html(text = None):
    # 將名稱、地址作爲鍵名、鍵值存入字典中
    video_urls_dic = {}
    
    video_urls = re.findall('<span class="xing_vb4"><a href="(.*?)" target="_blank">(.*?)</a></span>',text)
    
    # 如果去看一看原代碼中的地址,會發現,獲得的地址是殘缺的,需要手動拼接纔是真正的地址
    for video_url in video_urls:
        video_urls_dic[video_url[1]] = 'http://www.okzy.co/' + video_url[0]
    return video_urls_dic

# 將獲得的信息保存到本地,也可以直接動態的加載到一個列表裏而省去保存本地的操作
# 之所以這樣寫,是想保存一個播放過的記錄,以便下次可以從這裏接着播放
def get_message(keyword = None):

    with open('./video_url.txt', 'w', encoding='utf-8') as f:
        f.write('')
    with open('./search.txt','w',encoding = 'utf-8') as f:
        f.write('')
        
    video_dic = parse_html(text = download_url(video_name = keyword))
    for name in video_dic.keys():
        with open('./search.txt', 'a', encoding='utf-8') as f:
            f.write('{},{}'.format(name,video_dic[name]))
            f.write('\n')

選擇

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

# 把搜索到的電影打印出來,供選擇
def download_video_urls(url = None):
    with open('./search.txt', 'r', encoding='utf-8') as f:
        videos = f.readlines()

	# 字典中存放可以觀看的電影,但爲了方便選擇,以變量j爲鍵名
    video_dic = {}
    j = 1
	
	# 將存在的電影打印出,並放入字典
    for video in videos:
        video = video.split(',')
        print(str(j) + '\t' + video[0])
        video_dic[j] = video[1]
        j += 1
    
    # 選擇觀看的電影    
    while True:
        video_which = int(input('觀看的序號:'))
        if video_which <= len(videos):
            break
        else:
            print('輸入錯誤,檢查後再次輸入序號:')

	# 加載所選電影的版本即國語、英文或者電視劇的集數
    html = requests.get(url = video_dic[video_which]).text
    video_contents = re.findall('<li><input type="checkbox" name="copy_sel" value="(.*?)" checked="" />.*?</li> ',html)

	# 各種 if 是爲了篩選直接可以播放的電影
    for video_content in video_contents:
        if video_content != 'checkbox':
            if '.m3u8' not in video_content:
                if '.mp4' not in video_content:
                    with open('./video_url.txt','a',encoding = 'utf-8') as f:
                        f.write(video_content)
                        f.write('\n')

獲取鏈接

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

import webbrowser
# 這個函數在調用時,可以加一個循環,或者再添加一個功能獲取影片時長,實現自動下一集的播放
def watch_which():
    with open('./video_url.txt', 'r', encoding='utf-8') as f:
        videos = f.readlines()
    print('已更新 %s 集節目'%len(videos))
    num_page = int(input('觀看第幾集:'))

    with open('./record_last.txt','w',encoding = 'utf-8') as f:
        f.write(str(num_page))
        f.write('\t')
    webbrowser.open(url = videos[num_page - 1])

Android 端

程序運行環境

  1. COL-AL10
  2. Android 10
  3. QPython 3H

以上三個條件是程序調試的環境,必備條件是安卓系統蘋果系統不可用

三大模塊

搜索

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

# 下載網頁
def download_url(video_name = None):
    search_url = 'http://www.okzy.co/index.php?m=vod-search'
    from_data = {
        'wd':video_name,
        'submit':'search'
    }
    response = requests.get(url = search_url,params = from_data)
    response.encoding = 'utf-8'
    html = response.text
    return html
# 提取信息
def parse_html(text = None):
    video_urls_dic = {}
    video_urls = re.findall('<span class="xing_vb4"><a href="(.*?)" target="_blank">(.*?)</a></span>',text)
    for video_url in video_urls:
        video_urls_dic[video_url[1]] = 'http://www.okzy.co/' + video_url[0]
    return video_urls_dic

選擇

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

def download_video_urls(url = None):
    with open('./search.txt', 'r', encoding='utf-8') as f:
        videos = f.readlines()
    video_dic = {}
    j = 1
    for video in videos:
        video = video.split(',')
        print(str(j) + '\t' + video[0])
        video_dic[j] = video[1]
        j += 1
    while True:
        video_which = input('觀看的序號:')
        if len(video_which) != 0:
            video_which = int(video_which)
            if video_which <= len(videos):
                break
        else:
            print('輸入錯誤,檢查後再次輸入序號:')
    with open('./video_name.txt', 'w', encoding='utf-8') as f:
        f.write(videos[video_which - 1])
        
    html = requests.get(url = video_dic[video_which]).text
    video_contents = re.findall('<li><input type="checkbox" name="copy_sel" value="(.*?)" checked="" />.*?</li> ',html)
    for video_content in video_contents:
        if video_content != 'checkbox':
            if '.m3u8' not in video_content:
                if '.mp4' not in video_content:
                    with open('./video_url.txt','a',encoding = 'utf-8') as f:
                        f.write(video_content)
                        f.write('\n')

獲取鏈接

# Author:smart_num_1
# Blog:https://blog.csdn.net/smart_num_1

def watch_which(record = False,name = ''):
    with open('./video_url.txt', 'r', encoding='utf-8') as f:
        videos = f.readlines()
    if record:
        droid.startActivity("android.intent.action.VIEW",videos[record - 1])
        
    while True:
        os.system('clear')
        print('當前播放節目:%s'%name)
        print('已更新 %s 集節目'%len(videos))
        num_page = input('觀看第幾集(不輸入返回上一層):')
        if num_page == '':
            break        
        num_page = int(num_page)
        with open('./record_last.txt','w',encoding = 'utf-8') as f:
            f.write(str(num_page))
            f.write('\t')
        droid.startActivity("android.intent.action.VIEW",videos[num_page - 1])

成品展示

PC

本文章對應程序效果
在這裏插入圖片描述

搜索以及選擇觀看集數後,程序會自動調用瀏覽器進行播放

Android

在這裏插入圖片描述
在這裏插入圖片描述

PC端exe成品、源碼獲取方式

下載鏈接 提取碼: csnd

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章