Batch download of Youku videos using Python

Source: Internet
Author: User

Some time ago, due to the need to collect video data, I tried to download a youku video in batches.Program. Although things are simple, they are quite practical. I will share them with you.

Version: python2.7 + beautifulsoup3.2.1

 Import  Urllib, urllib2, sys, OS  From Beautifulsoup Import  Beautifulsoup  Import  Itertools, reurl_ I = 1 Pic_num = 1
# Self-defined Conversion Function of quotation mark format
Def _ En_to_cn (STR): OBJ = Itertools. cycle ([ ' " ' , ' " ' ]) _ OBJ = Lambda X: obj. Next () Return Re. sub (R " ['\ "] " , _ OBJ, STR) If _ Name __ = ' _ Main __ ' :
# Download video from three consecutive webpages
While Url_ I <= 3 : Webcontent = Urllib2.urlopen ( " Http://news.youku.com/focus/index/_page26716 _ " + STR (url_ I) + " . Html " ) Data = Webcontent. Read ()
# Use beautifulsoup to read Video list webpage data
Soup = Beautifulsoup (data) Print " ------------------------- Page " + STR (url_ I) + " ------------------------- " # Obtain the list of videos thumbnail and title on the corresponding page
Tag_list_thumb = Soup. findall ( ' Li ' , ' V_thumb ' ) Tag_list = Soup. findall ( ' Li ' , " V_title " ) For Item In Tag_list:# Guide the video playback page through herf in each thumbnail
Web_video_play = Urllib2.urlopen (item. [ ' Href ' ]) Data_vp = Web_video_play.read ()# Use beautifulsoup to read Video Playback webpage data
Soup_vp = Beautifulsoup (data_vp)# Find the link for "Download"
Tag_vp_list = Soup_vp.findall ( ' A ' , Id = ' Fn_download ' ) For Item_vp In Tag_vp_list:# Save the download link to url_dw
Url_dw = ' " ' + Item_vp [ ' _ Href ' ] + ' " ' Print Item. [ ' Title ' ] + " : " + Url_dw# Call the command line to run iku to download the video. You must add iku to the environment variable.
OS. System ( " Iku " + Url_dw)
# Save the thumbnail of each video For Item_thumb In Tag_list_thumb: urllib. urlretrieve (item_thumb.img [ ' SRC ' ], " E: \ Download video \ thumbnails \\ " + STR (pic_num) + " . " +_ En_to_cn (item_thumb.img [ ' Title ' ]) + " . Jpg " ) Pic_num + = 1 Print " -------------------------------------------------------------- " Print " -------- Page " + STR (url_ I) + " 'S VIDEO thumbnails have been saved! " Url_ I + = 1

The program idea is very simple, that is, to find the corresponding video playback webpage link by parsing the webpage data, and then find the download link based on the playback page, as shown in:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.