Python自定義豆瓣電影種類,排行,點評的爬取與存儲(進階下)

Python 2.7 
IDE Pycharm 5.0.3 
Firefox 47.0.1


如有興趣可以從如下幾個開始看起,其中有我遇到的很多問題: 
基礎抓取(限於“豆瓣高分”選項電影及評論)請看Python自定義豆瓣電影種類,排行,點評的爬取與存儲(基礎)

初級抓取(限於“豆瓣電影”的各種選項,包括“熱門”,“豆瓣高分”等十幾個類別及評論,並打包exe)請看Python自定義豆瓣電影種類,排行,點評的爬取與存儲(初級)

進階抓取(在初級抓取基礎上,套上GUI框架,圖像顯示更加人性化,並打包成exe)請看 Python自定義豆瓣電影種類,排行,點評的爬取與存儲(進階上)


我發現了,我不是在寫代碼,我是在寫BUG。。。

起因

不滿足於豆瓣電影的各種抓取,而把魔爪伸向了豆瓣電視劇,所以整合起來了整個豆瓣影視的抓取,所以以後要不要再抓豆瓣讀書啊。。。


目的

完成對豆瓣影視,包括豆瓣電影,豆瓣電視劇,豆瓣一週排行榜的自定義抓取及GUI界面設計及打包exe


方案

使用Tkinter+PhantomJS+Selenium+Firefox實現


實現過程

1.get到首頁後,根據選擇,點擊種類,然後根據輸入需求,進行排序 –這裏的輸入時listbox中值的點擊鍵入 
2.抓取每個電影及超鏈接,進入超鏈接後,抓取當前電影的熱評及長評 
3.當用戶所要求TOP數目大於第一頁的20個時候,點擊加載更多,再出現20個電影,重複2操作。 
4.將輸出寫入輸出框架中,寫入txt中等操作


實現效果

上圖吧,這裏第一項的影視選擇鍵都可以實現相關操作, 
這裏說明一下,第一項,影視選擇,第二項,類別選擇(電視劇看後面項),第三項,排序方式,第四項,是否加載評論,第五項TOP多少,第六項,保存名稱(需要格式爲xx.txt)

這裏寫圖片描述

下面是exe的版本

這裏寫圖片描述

已上傳資源,如有需要請點擊這裏進行下載


代碼

# -*- coding: utf-8 -*-
#Author:哈士奇說喵
#爬豆瓣高分影視及hot影評GUI完整版本

from selenium import webdriver
import selenium.webdriver.support.ui as ui
import time
from Tkinter import *


print "---------------system loading...please wait...---------------"

#獲取電影名及URL
def getURL_Title():
    global save_name
    SUMRESOURCES=0
    url="https://movie.douban.com/"
    driver_item=webdriver.Firefox()
    wait = ui.WebDriverWait(driver_item,15)

    Class_Dict={'Movies':1,'TV':2,'TOP10 M&T':3}
    #構建對應字典,方便鍵入值得對應關係查找
    Kind_Dict={'Hot':1,'Newest/American TV':2,'Classics/UK TV':3,'Playable/Korean TV':4,'High Scores/Japanese TV':5,
              'Wonderful but not popular/Chinese TV':6,'Chinese film/TVB':7,'Hollywood/Cartoon':8,
              'Korea':9,'Japan':10,'Action movies':11,'Comedy':12,'Love story':13,
              'Science fiction':14,'Thriller':15,'Horror film':16,'Whatever':17}
    #最後一個電影老是在變啊,艹
    Sort_Dict={'Sort by hot':1,'Sort by time':2,'Sort by score':3}
    Ask_Dict={'*No film reviews':0,'*I like film reviews':1}

    #鍵入的值對應,這幾個鍵不是必須的,比如說選擇TOP10 M&T時候
    try:
        kind=Kind_Dict[Kind_Select.get(Kind_Select.curselection()).encode('utf-8')]
        sort = Sort_Dict[Sort_Select.get(Sort_Select.curselection()).encode('utf-8')]
        number = int(input_Top.get())
    except:
        print 'if you are using TOP10 M&T ,it\'s all right\n if not,please choice kind/sort/number '

    class_MT = Class_Dict[MT_Select.get(MT_Select.curselection()).encode('utf=8')]
    ask_comments = Ask_Dict[Comment_Select.get(Comment_Select.curselection()).encode('utf-8')]
    save_name=input_SN.get()

    Ans.insert(END,"#####################################################################")
    Ans.insert(END,"                                                          Reloading                                           ")
    Ans.insert(END,",#####################################################################")
    Ans.insert(END,"---------------------------------------system loading...please wait...------------------------------------------")
    Ans.insert(END,"----------------------------------------------crawling----------------------------------------------")
    Write_txt('\n##########################################################################################','\n##########################################################################################',save_name)
    print "---------------------crawling...---------------------"

##############################################################################
#進行網頁get後,先進行電影種類選擇的模擬點擊操作,然後再是排序方式的選擇
#最後等待一會,元素都加載完了,才能開始爬電影,不然元素隱藏起來,不能被獲取
#wait.until是等待元素加載完成!
##############################################################################
    if class_MT==1:
        #選完參數後,開始爬操作
        driver_item.get(url)
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind))
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind).click()
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort))
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort).click()

        num=number+1#比如輸入想看的TOP22,那需要+1在進行操作,細節問題

        #打開幾次“加載更多”保險起見,多開一次加載
        num_time = num/20+2
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[20]"))
        for times in range(1,num_time):
            driver_item.find_element_by_xpath("//div[@class='list-wp']/a[@class='more']").click()
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[%d]"%(20*(times+1))))


        #使用wait.until使元素全部加載好能定位之後再操作,相當於try/except再套個while把

        for i in range(1,num):
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list']/a[%d]"%num))
            list_title=driver_item.find_element_by_xpath("//div[@class='list']/a[%d]"%i)
            print '----------------------------------------------'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------'
            print u'電影名: ' + list_title.text
            print u'鏈接: ' + list_title.get_attribute('href')
            #print unicode碼自動轉換爲utf-8的

            #防止頁面出現重複錯誤,增加迭代修正
            while list_title.text==driver_item.find_element_by_xpath("//div[@class='list']/a[%d]"%(i+20)).text:
                print u'遇到頁面加載重複項bug,開始重新加載...'
                driver_item.quit()
                getURL_Title()

            #list_title.text是unicode碼,需要重新編碼再寫入txt
            list_title_wr=list_title.text.encode('utf-8')
            list_title_url_wr=list_title.get_attribute('href')
            #寫入gui的輸出框中
            Ans.insert(END,'\n-------------------------------------------Movies--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------',list_title_wr,list_title_url_wr)
            #寫入txt中
            Write_txt('\n------------------------------------------Movies--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------','',save_name)
            Write_txt(list_title_wr,list_title_url_wr,save_name)

            SUMRESOURCES = SUMRESOURCES +1


            #獲取具體內容和評論。href是每個超鏈接也就是資源單獨的url
            try:
                getDetails(str(list_title.get_attribute('href')),ask_comments)
            except:
                print 'can not get the details!'

        #爬完數據後關閉瀏覽器,只保留GUI進行下一步操作
        driver_item.quit()


    #選擇電視劇之後的操作
    if class_MT == 2:
        #選完參數後,開始爬操作,選電視劇需要多點擊一次,因爲默認爲電影
        driver_item.get(url)
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/h2/a[2]"))
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/h2/a[2]").click()

        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind))
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind).click()
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort))
        driver_item.find_element_by_xpath("//div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort).click()

        num=number+1#比如輸入想看的TOP22,那需要+1在進行操作,細節問題


        #打開幾次"加載更多",因爲要解決點擊後頁面可能重複的問題,所以這裏多點一次
        num_time = num/20+2
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[20]"))
        for times in range(1,num_time):
            driver_item.find_element_by_xpath("//div[@class='list-wp']//a[@class='more']").click()
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[%d]"%(20*(times+1))))

        #使用wait.until使元素全部加載好能定位之後再操作,相當於try/except再套個while把

        for i in range(1,num):
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list']/a[%d]"%num))
            list_title=driver_item.find_element_by_xpath("//div[@class='list']/a[%d]"%i)
            print '-------------------------------------------TV--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------'
            print u'電視劇名: ' + list_title.text
            print u'鏈接: ' + list_title.get_attribute('href')
            #print unicode碼自動轉換爲utf-8的


            #防止頁面出現重複錯誤,增加迭代修正
            while list_title.text==driver_item.find_element_by_xpath("//div[@class='list']/a[%d]"%(i+20)).text:
                print u'遇到頁面加載重複項bug,開始重新加載...'
                driver_item.quit()
                getURL_Title()


            #list_title.text是unicode碼,需要重新編碼再寫入txt
            list_title_wr=list_title.text.encode('utf-8')
            list_title_url_wr=list_title.get_attribute('href')
            #寫入gui的輸出框中
            Ans.insert(END,'\n------------------------------------------TV--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------',list_title_wr,list_title_url_wr)
            #寫入txt中
            Write_txt('\n----------------------------------------TV--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------','',save_name)
            Write_txt(list_title_wr,list_title_url_wr,save_name)

            SUMRESOURCES = SUMRESOURCES +1


            #獲取具體內容和評論。href是每個超鏈接也就是資源單獨的url
            try:
                getDetails(str(list_title.get_attribute('href')),ask_comments)
            except:
                print 'can not get the details!'

        #爬完數據後關閉瀏覽器,只保留GUI進行下一步操作
        driver_item.quit()


    #本週口碑榜TOP10
    if class_MT==3:
        #選完參數後,開始爬操作
        driver_item.get(url)

        for i in range(1,11):
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='billboard-bd']/table/tbody/tr"))
            list_title=driver_item.find_element_by_xpath("//div[@class='billboard-bd']/table/tbody/tr[%d]/td[2]/a"%i)
            print '----------------------------------------------'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------'
            print u'影視名: ' + list_title.text
            print u'鏈接: ' + list_title.get_attribute('href')
            #print unicode碼自動轉換爲utf-8的

            #list_title.text是unicode碼,需要重新編碼再寫入txt
            list_title_wr=list_title.text.encode('utf-8')
            list_title_url_wr=list_title.get_attribute('href')
            #寫入gui的輸出框中
            Ans.insert(END,'\n-------------------------------------------WeekTOP--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------',list_title_wr,list_title_url_wr)
            #寫入txt中
            Write_txt('\n------------------------------------------WeekTOP--'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------','',save_name)
            Write_txt(list_title_wr,list_title_url_wr,save_name)

            SUMRESOURCES = SUMRESOURCES +1


            #獲取具體內容和評論。href是每個超鏈接也就是資源單獨的url
            try:
                getDetails(str(list_title.get_attribute('href')),ask_comments)
            except:
                print 'can not get the details!'

        #爬完數據後關閉瀏覽器,只保留GUI進行下一步操作
        driver_item.quit()



##############################################################################
#當選擇一部電影后,進入這部電影的超鏈接,然後才能獲取
#同時別忽視元素加載的問題
#在加載長評論的時候,注意模擬點擊一次小三角,不然可能會使內容隱藏
##############################################################################

def getDetails(url,comments):
    driver_detail = webdriver.PhantomJS(executable_path="phantomjs.exe")
    wait1 = ui.WebDriverWait(driver_detail,15)
    driver_detail.get(url)
    wait1.until(lambda driver: driver.find_element_by_xpath("//div[@id='link-report']/span"))
    drama = driver_detail.find_element_by_xpath("//div[@id='link-report']/span")
    print u"劇情簡介:"+drama.text

    drama_wr=drama.text.encode('utf-8')
    #寫入gui的輸出框中
    Ans.insert(END,drama_wr)
    #寫入到txt
    Write_txt(drama_wr,'',save_name)

    #加載評論
    if comments == 1:

        print "--------------------------------------------Hot comments TOP----------------------------------------------"
        #加載四個短評
        for i in range(1,5):
            try:
                comments_hot = driver_detail.find_element_by_xpath("//div[@id='hot-comments']/div[%s]/div/p"%i)
                print u"最新熱評:"+comments_hot.text
                comments_hot_wr=comments_hot.text.encode('utf-8')
                Ans.insert(END,"--------------------------------------------Hot comments TOP%d----------------------------------------------"%i,comments_hot_wr)
                Write_txt("--------------------------------------------Hot comments TOP%d----------------------------------------------"%i,'',save_name)
                Write_txt(comments_hot_wr,'',save_name)
            except:
                print 'can not caught the comments!'

        #嘗試加載長評
        try:
            driver_detail.find_element_by_xpath("//img[@class='bn-arrow']").click()
            #wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='review-bd']/div[2]/div/div"))
            time.sleep(1)

            #解決加載長評會提示劇透問題導致無法加載
            comments_get = driver_detail.find_element_by_xpath("//div[@class='review-bd']/div[2]/div")
            if comments_get.text.encode('utf-8')=='提示: 這篇影評可能有劇透':
                comments_deep=driver_detail.find_element_by_xpath("//div[@class='review-bd']/div[2]/div[2]")
            else:
                comments_deep = comments_get

            print "--------------------------------------------long-comments---------------------------------------------"
            print u"深度長評:"+comments_deep.text
            comments_deep_wr=comments_deep.text.encode('utf-8')
            #寫入gui的輸出框中
            Ans.insert(END,"--------------------------------------------long-comments---------------------------------------------\n",comments_deep_wr)
            Write_txt("--------------------------------------------long-comments---------------------------------------------\n",'',save_name)
            Write_txt(comments_deep_wr,'',save_name)

        except:
            print 'can not caught the deep_comments!'


##############################################################################
#將print輸出的寫入txt中查看,也可以在cmd中查看,換行符是爲了美觀
##############################################################################
def Write_txt(text1='',text2='',title='douban.txt'):

        with open(title,"a") as f:
            for i in text1:
                f.write(i)
            f.write("\n")
            for j in text2:
                f.write(j)
            f.write("\n")


def Clea():#清空函數
    input_Top.delete(0,END)#這裏entry的delect用0
    input_SN.delete(0,END)
    Ans.delete(0,END)#text中的用0.0
    Ans.insert(END,"Pay Attention: ","if you are using 'Movies'or 'TV',please select all parameters,and 'TV' share the same parameters but use the latter one","if you are using 'TOP10 M&T', please select parameters with '*'",'####################################################################')


#------------------------------------------------------------------------------------------
#------------------------------------------GUI操作界面--------------------------------------
#-----------------------------------------------------------------------------------------

root=Tk()
root.title('豆瓣影視抓取器1.0--by哈士奇說喵')
frame_select=Frame(root)
title_label=Label(root,text='豆瓣影視TOP抓取器')
title_label.pack()

#---------------選擇電影/電視劇-------------------
MT_Select=Listbox(frame_select,exportselection=False,width=12,height=4)
list_item1 = ['Movies','TV','TOP10 M&T']
for i in list_item1:
    MT_Select.insert(END,i)

scr_MT = Scrollbar(frame_select)
MT_Select.configure(yscrollcommand = scr_MT.set)
scr_MT['command']=MT_Select.yview


#---------------選擇電影/電視劇   種類-------------------

Kind_Select=Listbox(frame_select,exportselection=False,width=22,height=4)
list_item2 = ['Hot','Newest/American TV','Classics/UK TV','Playable/Korean TV','High Scores/Japanese TV',
              'Wonderful but not popular/Chinese TV','Chinese film/TVB','Hollywood/Cartoon',
              'Korea','Japan','Action movies','Comedy','Love story',
              'Science fiction','Thriller','Horror film','Whatever']


for i in list_item2:
    Kind_Select.insert(END,i)

scr_Kind = Scrollbar(frame_select)
Kind_Select.configure(yscrollcommand = scr_Kind.set)
scr_Kind['command']=Kind_Select.yview


#---------------選擇電影/電視劇  排序方式-------------------

Sort_Select=Listbox(frame_select,exportselection=False,width=12,height=4)
list_item3 = ['Sort by hot','Sort by time','Sort by score']
for i in list_item3:
    Sort_Select.insert(END,i)

scr_Sort = Scrollbar(frame_select)
Sort_Select.configure(yscrollcommand = scr_Sort.set)
scr_Sort['command']=Sort_Select.yview

#---------------選擇電影/電視劇/本週口碑TOP10   是否加載評論-------------------

Comment_Select=Listbox(frame_select,exportselection=False,width=16,height=4)
list_item4 = ['*No film reviews','*I like film reviews']
for i in list_item4:
    Comment_Select.insert(END,i)

scr_Com = Scrollbar(frame_select)
Comment_Select.configure(yscrollcommand = scr_Com.set)
scr_Com['command']=Comment_Select.yview


#---------------選擇電影/電視劇   選擇TOP數-------------------
Label_TOP=Label(frame_select, text='TOP(xx)', font=('',10))
var_Top = StringVar()
input_Top = Entry(frame_select, textvariable=var_Top,width=8)


#---------------選擇電影/電視劇/本週口碑TOP10   保存路徑-------------------
Label_SN=Label(frame_select, text='*SAVE_NAME(xx.txt)', font=('',10))
var_SN = StringVar()
input_SN = Entry(frame_select, textvariable=var_SN,width=8)



#----------------------------------------------輸出框架-----------------------------------------
frame_output=Frame(root)
out_label=Label(frame_output,text='Details')
Ans = Listbox(frame_output,selectmode=MULTIPLE, height=15,width=100)#text也可以,Listbox好處在於換行
Ans.insert(END,"Pay Attention: ","if you are using 'Movies'or 'TV',please select all parameters,and 'TV' share the same parameters but use the latter one","if you are using 'TOP10 M&T', please select parameters with '*'",'####################################################################')

#點擊crawl_button就進入getURL_Title(),點擊clear_button就進入Clea()
crawl_button = Button(frame_output,text='crawl', command=getURL_Title)
clear_button = Button(frame_output,text='clear', command=Clea)


#縱向拖拽
scr_Out_y = Scrollbar(frame_output)
Ans.configure(yscrollcommand = scr_Out_y.set)
scr_Out_y['command']=Ans.yview
#橫向拖拽
scr_Out_x = Scrollbar(frame_output,orient='horizontal')#ans x
Ans.configure(xscrollcommand = scr_Out_x.set)
scr_Out_x['command']=Ans.xview


#----------------------------------------------顯示框架-----------------------------------------

#----------------選擇框架顯示--------------
frame_select.pack()
#影視框架顯示
MT_Select.pack(side=LEFT)
scr_MT.pack(side=LEFT)
#種類框架顯示
Kind_Select.pack(side=LEFT)
scr_Kind.pack(side=LEFT)
#排序框架顯示
Sort_Select.pack(side=LEFT)
scr_Sort.pack(side=LEFT)
#評論框架顯示
Comment_Select.pack(side=LEFT)
scr_Com.pack(side=LEFT)
#TOP輸入
Label_TOP.pack()
input_Top.pack()
#SAVE NAME輸入
Label_SN.pack()
input_SN.pack()

#----------------輸出框架顯示--------------
frame_output.pack()
out_label.pack()
crawl_button.pack(side=LEFT)

clear_button.pack(side=RIGHT)
scr_Out_y.pack(side=RIGHT)
Ans.pack()
scr_Out_x.pack()
#----------------根框架顯示--------------
root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420

修復問題

相比較比上幾次版本,改進了 
1.刪除無關的顯性等待時間,直接全部靠wait.until來實現元素加載完成標識。 
比如修改部分核心代碼改變爲如下:

#打開幾次“加載更多”保險起見,多開一次加載
        num_time = num/20+2
        wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[20]"))
        for times in range(1,num_time):
            driver_item.find_element_by_xpath("//div[@class='list-wp']/a[@class='more']").click()
            wait.until(lambda driver: driver.find_element_by_xpath("//div[@class='list-wp']/div/a[%d]"%(20*(times+1))))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.針對豆瓣頁面可能存在”加載更多”之後重複頁面現象,增加了自動刷新修復頁面(其實就是採用迭代,自己重新加載),所以可能會遇到這種情況(詳見僞解決Selenium中調用PhantomJS無法模擬點擊(click)操作 

遇到頁面加載重複項bug,開始重新加載...
---------------------crawling...---------------------
  • 1
  • 2
  • 1
  • 2

不用擔心,這是自動在進行重加載和修復。

核心代碼是

#防止頁面出現重複錯誤,重複錯誤形式請看,增加迭代修正
            while list_title.text==driver_item.find_element_by_xpath("//div[@class='list']/a[%d]"%(i+20)).text:
                print u'遇到頁面加載重複項bug,開始重新加載...'
                driver_item.quit()#關閉當前頁面,重新來
                getURL_Title()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

3.對新增TV選項,做了一些調整,代碼太長,就不放上來了,可以從源碼看,主要還是增加判斷選項,然後和Movies照樣畫葫蘆


4.對新增TOP10 M&T,根據其網頁特性,進行點擊和抓取操作,代碼見源碼


5.對輸入的問題,因爲新增的TOP10 M&T,不需要TOP多少及其他kind及sort等選項,所以增加try/except選項,可以選擇輸入和不輸入,當然,電影和電視時候還是要輸入的,不然參數都沒有就跑不了了,這裏會在GUI上打印出操作須知

#鍵入的值對應,這幾個鍵不是必須的,比如說選擇TOP10 M&T時候
    try:
        kind=Kind_Dict[Kind_Select.get(Kind_Select.curselection()).encode('utf-8')]
        sort = Sort_Dict[Sort_Select.get(Sort_Select.curselection()).encode('utf-8')]
        number = int(input_Top.get())
    except:
        print 'if you are using TOP10 M&T ,it\'s all right\n if not,please choice kind/sort/number '

    class_MT = Class_Dict[MT_Select.get(MT_Select.curselection()).encode('utf=8')]
    ask_comments = Ask_Dict[Comment_Select.get(Comment_Select.curselection()).encode('utf-8')]
    save_name=input_SN.get()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

還有一個問題

我能實時在cmd窗口看到抓到的數據,但是在gui的輸出窗口卻看不到,只能等到cmd全部跑完纔可以,我也不知道問題出在哪裏,可能和線程有關?我下次嘗試分一個單獨線程給gui跑輸入,看看是不是這個問題,我想我的語句應該沒有什麼問題,如果有人看出問題所在了,請留言告知,謝過


致歉

對以前的版本,有很多錯誤,影響大家進行測試,我深表抱歉,對未充分進行測試,這鍋我背,所以我花了好多時間看電影,啊,不,花了好多時間測試,大概每個都進行了TOP40+的測試,應該沒有太大問題的,有時候電腦GUI會顯示未響應,請不要擔心,這個正常現象。


最後

我應該多讀點別人的程序,閉門造車和自己研究是兩回事,取長補短再深入研究纔是正道。


致謝

Python自定義豆瓣電影種類,排行,點評的爬取與存儲(基礎) 
Python自定義豆瓣電影種類,排行,點評的爬取與存儲(初級) 
Python自定義豆瓣電影種類,排行,點評的爬取與存儲(進階上)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章