Python爬蟲:給我一個連結,虎牙影片隨便下載

專注的阿熊發表於2021-09-22

import requests

from lxml import etree

from crawlers.userAgent import useragent

import re

import json

import time

class video(object):

     def __init__(self,url):

         # url 為輸入的連結

         self.url = url

         self.page = 0

         self.u = useragent()

     def getPages(self):

         # 獲取當前連結介面的總頁數

         headers = {'user-agent':self.u.getUserAgent()}

         rsp = requests.get(url=self.url,headers=headers)

         html = etree.HTML(rsp.text)

         aList = html.xpath('//div[@class="user-paginator"]/ul/li/a')

         print(' 影片總頁數為外匯跟單gendan5.com: ',aList[-2].text)

         self.page = int(input(" 請輸入想下載的影片頁數: "))

     def downloadVideo(self):

         # 下載影片的方法 , 並沒有下載影片,只是獲取影片的下載連結

         for i in range(1,self.page+1):

             if i == 1:

                 url2 = '{}?sort=news'.format(self.url)

             else:

                 url2 = '{}?sort=news&p={}'.format(self.url,i)

             headers = {'user-agent':self.u.getUserAgent()}

             rsp = requests.get(url=url2,headers=headers)

             html2 = etree.HTML(rsp.text)

             hrefs = html2.xpath('//div[@class="content-list"]/ul/li/a')

             for j in range(len(hrefs)):

                 href = hrefs[j].xpath('./@href')[0]

                 title = hrefs[j].xpath('./@title')[0]

                 print(' 影片名稱為: ',title)

                 vid = re.findall("/play/(\d*).html",href)[0]   # 獲取 vid

                 self.getDownloadHref(vid=vid)

                 print('#'*50)

             time.sleep(2)

     def getDownloadHref(self,vid):

         url3 = '

         params={'callback': 'jQuery1124017458848743440036_1632126349635',

                 'r': 'vhuyaplay/video',

                 'vid': vid,

                 'format': 'mp4,m3u8',

                 '_': '1632126349643'}

         rsp = requests.get(url=url3,headers={'user-agent':self.u.getUserAgent()},params=params)

         infos = rsp.text

         lindex = infos.find('(')

         rindex = infos.find(')')

         dict2 = json.loads(infos[lindex+1:rindex])

         list2 = dict2['result']['items']

         v_list2=[' 高畫質 ',' 原畫 ',' 流暢 ']

         for i in range(len(list2)):

             print(v_list2[i],list2[i]['transcode']['urls'][0])

if __name__ == '__main__':

     url = input(" 請輸入影片連結: ")

     v = video(url)

     v.getPages()

     v.downloadVideo()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2793088/,如需轉載,請註明出處,否則將追究法律責任。

相關文章