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: