用python抓取在线视频地址
喜欢优酷的视频,但是要下载它的客户端才能下载在线的视频,这一点很多朋友和妹纸都觉得很不爽,我为了自己练手自己写了一个解析视频地址的小工具。。。。反正也不是什么高科技,公开一下源代码,让大家学习一下。。。
1 import re 2 import sys 3 import urllib 4 import urllib2 5 import datetime 6 from win32clipboard import * 7 from win32con import CF_TEXT 8 9 def get_Clipboard(): 10 OpenClipboard() 11 text = GetClipboardData(CF_TEXT) 12 CloseClipboard() 13 return text 14 15 16 17 class CFlvcd(object): 18 def __init__(self): 19 self.url = "" 20 self.pattern = re.compile(r"<a href *= *\"(http://f\.youku\.com/player/getFlvPath/[^\"]+)") 21 self.headers = {"Accept":"*/*", "Accept-Language":"zh-CN", "":"", 22 "User-Agent":"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)", 23 #"Accept-Encoding":"gzip, deflate", 24 "Connection":"Keep-Alive"} 25 26 def parse(self, url): 27 self.url = "http://www.flvcd.com/parse.php?kw=" + url + "&format=super" 28 req = urllib2.Request(url=self.url, headers=self.headers) 29 res = urllib2.urlopen(req) 30 data = res.read() 31 re_res = self.pattern.findall(data) 32 if re_res != None: 33 filename = datetime.datetime.now().strftime("%Y%m%d-%H%M%S.lst") 34 fhandle = open(filename, "w") 35 for url in re_res: 36 # 注意是\r\n还是\n 37 fhandle.write(url + "\n") 38 fhandle.close() 39 print("Parse URL Done!") 40 else: 41 print("URL Not Found") 42 43 def main(): 44 flvcd=CFlvcd() 45 print'你要下载的视频地址是' 46 print get_Clipboard() 47 print'确定获取请按1' 48 a=raw_input() 49 if (a=='1'): 50 flvcd.parse(get_Clipboard()) 51 52 53 54 55 if __name__ == "__main__": 56 main()