学习python简单爬虫小程序,记录代码和学习过程
python 2.7.12
查看网页代码:
通过这段代码,我们可以考虑分析。在python代码中用一段存放描述内容,一段存放视频所在的url 对于每个url地址,通过urllib所提供的urlretrieve下载
# -*- encoding:utf8 -*-
import re
import sys
import urllib2
import urllib
import os
reload(sys)
sys.setdefaultencoding("utf-8")
#a = 1
url_name = []
def get(pageindex):
url = 'http://www.budejie.com/video/' + str(pageindex)
# var1.set('已经获取到第%s页的视频视频'%(a))
print url
html = urllib.urlopen(url).read()
url_reg = r'data-mp4="(.*?)"'
url_items = re.findall(url_reg, html)
name_reg = re.compile('<div class="j-r-list-c-desc".*?<a href=".*?>(.*?)</a>.*?</div>', re.S)
name_items = re.findall(name_reg, html)
for i, k in zip(name_items, url_items):
url_name.append([i, k])
#传入文件名和video地址
def saveVideo(filename,videoUrl):
print 'Saving : %s ...'%filename
urllib.urlretrieve(videoUrl,'D:\\video\\%s.mp4'%filename)
####main exec ####
for pageindex in range(1,3):
get(pageindex)
for index,item in enumerate(url_name):
saveVideo(index,item[1])