標籤:
1 # coding:utf-8 2 print(‘正在初始化...‘) 3 import requests 4 import re 5 from lxml.html import fromstring 6 import pyautogui 7 import sys 8 import os 9 from multiprocessing.dummy import Pool10 from lxml import _elementpath11 12 13 class Youku_comment:14 15 """docstring for Youku_comment"""16 17 def __init__(self, raw_url):18 19 self.pid = re.findall(‘/id_(.*?)\.html‘, raw_url)[0]20 r1 = requests.get(raw_url)21 title = re.findall(‘<title>(.*?)</title>‘, r1.text)[0]22 title = re.sub(‘\W‘, ‘‘, title).replace(‘線上播放優酷網視頻高清線上觀看‘, ‘‘)23 totalpn = self.get_totalpn(self.pid)24 print(‘視頻ID:%s‘ % self.pid, ‘\n視頻標題:%s‘ %25 title, ‘\n總頁碼數:%s\n正在抓取...‘ % totalpn)26 27 pp = Pool(30)28 pagenums = range(1, totalpn + 1)29 result = pp.map(self.get_comment, pagenums)30 pp.close()31 pp.join()32 result = [i for i in result if i]33 self.aa = sum(result, [])34 # print(jieguo)35 # with open(‘%s.csv‘ % title, ‘w‘, encoding=‘gbk‘) as f:36 # f.write(jieguo.encode(‘gbk‘, ‘ignore‘).decode(‘gbk‘))37 38 def get_totalpn(self, pid):39 r = requests.get(40 ‘http://comments.youku.com/comments/~ajax/vpcommentContent.html?__ap={"videoid":"%s","page":1}‘ % pid)41 totalpn = (int(r.json()[‘totalSize‘].replace(‘,‘, ‘‘)) // 30) + 142 return totalpn43 44 def get_comment(self, pagenum):45 for _ in range(5):46 try:47 r = requests.get(48 ‘http://comments.youku.com/comments/~ajax/vpcommentContent.html?__ap={"videoid":"%s","page":%s}‘ % (self.pid, pagenum), timeout=3)49 sjson = r.json()50 scode = sjson[‘con‘]51 ss = re.findall(‘<p id=".*?">.*?</p>‘, scode, flags=re.S)52 ss = [re.sub(‘<.*?>‘, ‘‘, i) for i in ss]53 if ss:54 return ss55 except:56 pass57 58 59 def filt1(str1, kws):60 kws = kws.split(‘ ‘) if kws else ‘OST 背景 音樂 旋律 歌曲 調子 music 耳熟 BGM 誰唱的 來自 出自 原聲‘.split(61 ‘ ‘)62 for i in kws:63 if i in str1:64 return str165 66 67 def quchong(ll):68 ss = ‘‘69 for i in ll:70 if i in ss:71 continue72 else:73 ss = ss + ‘\n‘ + i74 return ss75 76 while 1:77 try:78 url = pyautogui.prompt(‘請輸入網址:‘)79 if not url:80 break81 tt = Youku_comment(url)82 pinglun = tt.aa83 while 1:84 kws = pyautogui.prompt(‘請輸入關鍵詞,多個請用空格隔開(直接斷行符號則代表找背景音樂):‘)85 kws = kws if kws else 086 ss = [filt1(i, kws) for i in pinglun]87 ss = [i for i in ss if i]88 ss = quchong(ss)89 print(‘檢索結果:\n‘)90 print(ss)91 jixu = pyautogui.confirm(92 text=‘是否要繼續檢索‘, title=‘請確認‘, buttons=[‘是‘, ‘否‘])93 if jixu == ‘否‘:94 break95 96 except Exception as e:97 print(e)98 print(‘錯誤,請重試‘)99 os.system(‘pause‘)
Windows已編譯可執行檔: http://pan.baidu.com/s/1bn0jLmf
Python練習:優酷評論過濾(抓取當前視頻全部評論,並過濾不包括所需關鍵詞的留言)