download startup video in xlzhao

weixin_33806914發表於2017-04-27
#coding=utf-8
import re,urllib2,urllib,json,time,os
from tomorrow import threads
# @threads(10)

def getHtmlCode(url):
    return urllib2.urlopen(url).read()

def getStartupVideo():
    for i in range(1,140):
        print str(i)
        url = 'http://api.xlzhao.com/v2/videos/channel/10/second_id/0/hot?page=' + str(i)
        htmlCode = getHtmlCode(url)
        decodeJson = json.loads(htmlCode)
        itemList = decodeJson["data"]["item"]
        for j in range(0,len(itemList)):
            data = itemList[j]
            name = data["name"]
            play_num = data["play_num"]
            file_url = data["file_url"]
            fileName = str(play_num) + '-' + name.encode('utf-8')
            try:
                urllib.urlretrieve(file_url,"%s.mp4" %("startup/" + fileName))
                print (fileName + '---done')
            except:
                print (fileName + '---error')

getStartupVideo()

相關文章