python幾乎無所不能 只有你不知道的,如何通過Python玩轉小視頻

來源:互聯網
上載者:User

標籤:模組使用   交流   stat   正則   urllib   port   image   資料   coding   

 

什麼是爬蟲?

就是抓取網頁資料的程式

爬蟲怎麼抓取網頁資料?

網頁三大特徵:

  • 網頁都有自己唯一的URL。
  • 網頁都是HTML來描述頁面資訊。
  • 網頁都使用http/https協議來傳輸HTML資料。

爬蟲的設計思路:

  • 擷取視頻ID
  • 拼接完整url
  • 擷取視頻播放地址
  • 下載視頻

模組使用 requests

安裝“pip install requests”

Requests庫的七個主要方法

 

找到單個視頻播放地址

 

擷取網頁原始碼

 

擷取播放地址

 

下載視頻

 

  完整代碼
 1 # -*- coding:utf-8 -*-  2 import requests 3 from lxml import etree 4 import re 5 from urllib.request import urlretrieve 6 #擷取視頻ID 7 #拼接完整url 8 #擷取視頻播放地址 9 #下載視頻10 #Python學習交流群:125240963,群內每天分享乾貨,包括最新的python企業案例學習資料和零基礎入門教程,歡迎各位小夥伴入群學習交流11 12 def download(url):13     #url = ‘http://www.pearvideo.com/category_9‘14     #擷取原始碼15     #html = requests.get(url)16     html = requests.get(url).text17     #把文字檔處理成可解釋的對象18     #如果我用正則可以不可以也可以19     html = etree.HTML(html)20     video_id = html.xpath(‘//div[@class="vervideo-bd"]/a/@href‘)21     video_url = []22     starurl = ‘http://www.pearvideo.com‘23     #完整拼接url24     for id in video_id:25         newurl = starurl + ‘/‘ + id26         video_url.append(newurl)27     #擷取視頻播放地址28     for playurl in video_url:29         #擷取頁面原始碼30         html = requests.get(playurl).text31         #print(playurl)32         req = ‘srcUrl="(.*?)"‘33         #視頻真正播放地址34         purl = re.findall(req,html)35         #print(purl)36         #擷取視頻名稱37         req = ‘<h1 class="video-tt">(.*?)</h1>‘38         pname = re.findall(req,html)39         print("正在下載:%s"%pname)40 41         urlretrieve(purl[0],‘./video/%s.mp4‘%pname[0])42 #download()43 44 def downloadmore():45     n = 1246     while True:47         if n > 48:48             return49         url = "http://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=9&star=%d"%n50         n+=1251         download(url)52 downloadmore()

 

python幾乎無所不能 只有你不知道的,如何通過Python玩轉小視頻

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.