1. 程式人生 > >進程池 爬去梨視頻 視頻資源

進程池 爬去梨視頻 視頻資源

test split 二進制 write 路徑 import layer get edi

# 導入requests網絡請求模塊
import requests
# 導入lxml標簽匹配模塊
from lxml import etree
# 導入re 正則匹配模塊
import re
#導入系統路徑模塊
import os
# 導入進程模塊
import multiprocessing

# 存在視頻網址
mylist = []

# 請求函數
def Data(url):
    #發送請求
    test = requests.get(url)
    # with open(‘./pa.html‘,‘w‘) as pa:
    #     pa.write(test.text.encode(‘gbk‘,‘ignore‘).decode(‘gbk‘,‘ignore‘))
# 返回二進制流 return test.content # 匹配標簽函數 def Pipa(): # 調用請求函數 res = Data(https://www.pearvideo.com/category_10) # 利用etree完整HTML數據 html = etree.HTML(res) # 匹配標簽 url = html.xpath(//*[@id="categoryList"]/li) # 循環匹配到的標簽進行操作 for i in url: # 拼接完整的詳情頁網址
data = https://www.pearvideo.com/ + str(i.xpath(./div/a/@href)[0]) # 添加到準備好的列表內 mylist.append(data) # 定義好寫入方法 def xiangqing(url): # 獲取url切片用於視頻名稱 name = str(url).split(/)[-1] + .mp4 print(name) # 調用請求方法獲取詳情頁 res = Data(url) #由於視頻網址不在標簽裏而是在Jquery內所有沒辦法使用xpath 這裏使用re匹配視頻源所在網址
url = re.findall( srcUrl="(.*?)",vdoUrl=srcUrl,skinRes="//www.pearvideo.com/domain/skin",videoCDN="//video.pearvideo.com";, str(res))[0] # 調用請求方法把視頻所在的網址放進去獲取資源 res = Data(url) # 設置路徑 path = "C:/Users/nxy/Videos/PLAYERUNKNOWN‘S BATTLEGROUNDS/" # 判斷路徑是否存在 if not os.path.exists(path): # 不存在則創建 os.makedirs(path) #寫入 with open(path + name, "wb") as f: f.write(res) if __name__ == "__main__": #調用進行添加列表 Pipa() print(mylist) #調用進程池 pool = multiprocessing.Pool(4) # 用map方法傳參調用進程池 pool.map(xiangqing, mylist)

進程池 爬去梨視頻 視頻資源