Use Python to implement the Youku video batch download function instance

Source: Internet
Author: User
Some time ago, due to the need to collect video data, I had a program for batch downloading YouKu videos. The following small series share the implementation process to the platform for your reference. some time ago, due to the need to collect video data, you have created a program for batch downloading YouKu videos. Although things are simple, they are quite practical. I will share them with you.

Version: Python2.7 + BeautifulSoup3.2.1

Import urllib, urllib2, sys, osfrom BeautifulSoup import BeautifulSoupimport itertools, reurl_ I = 1pic_num = 1 # define your own quote format conversion function def _ en_to_cn. 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 tag_list_thumb = soup of the video thumbnail and title on the corresponding page. findAll ('Lil', 'V _ thumb') tag_list = soup. findAll ('Lil', "v_title") for item in tag_list: # guide the video playback page web_video_play = urllib2.urlopen (item. a ['href ']) Data_vp = web_video_play.read () # use BeautifulSoup to read video playback webpage data soup_vp = BeautifulSoup (data_vp) # find the link tag_vp_list = download ('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. a ['title'] + ":" + url_dw # Call the command line to run iku to download the video. you need to add iku to the environment variable OS. system ("iku" + url_dw) # save the thumbnail for item_thumb in tag_list_thumb: urllib. u Rlretrieve (item_thumb.img ['src'], "E :\\ download video \ thumbnails \" + str (pic_num) + ". "+ _ en_to_cn (item_thumb.img ['title']) + ". jpg ") pic_num + = 1 print" bytes "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:

The download link obtained from the webpage data must be downloaded through the youku iku. It took me a lot of trouble to find that the command line of iku is very simple (you can directly use iku download_link), so the simplest way is to use the command line interface OS in Python. system to call iku to download the video. In addition, you must start iku before running the program. Otherwise, you must start another video after downloading it.

PS: When downloading videos, you will find that these video webpages in China are not very detailed enough and contain too many duplicate links and necrotic links.

The above is a detailed example of using Python to implement the Youku video batch download function. For more information, see other related articles in the first PHP community!

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.