目的:
1.根據(jù)用戶輸入,列出豆瓣高分TOP(用戶自定義)的電影,鏈接,及熱評若干
方案:
使用PhantomJS+Selenium+Firefox實現(xiàn)
實現(xiàn)過程:
1.get到首頁后,根據(jù)選擇,點擊種類,然后根據(jù)輸入需求,進行排序
2.抓取每個電影及超鏈接,進入超鏈接后,抓取當前電影的熱評及長評
3.當用戶所要求TOP數(shù)據(jù)大于第一頁的20個時候,點擊加載更多,再出現(xiàn)20個電影,重復2操作
以豆瓣發(fā)哦分,然后按評分排序的點擊過程(其余操作一致,先種類后排序選擇,再爬)
image
實現(xiàn)代碼:
# -*- coding: utf-8 -*-
#Author:哈士奇說喵
#爬豆瓣高分電影及hot影評
from selenium import webdriver
import selenium.webdriver.support.ui as ui
import time
print "---------------system loading...please wait...---------------"
SUMRESOURCES = 0 #全局變量
driver_detail = webdriver.PhantomJS(executable_path="phantomjs.exe")
#driver_item=webdriver.PhantomJS(executable_path="phantomjs.exe")
driver_item=webdriver.Firefox()
url="https://movie.douban.com/"
#等待頁面加載方法
wait = ui.WebDriverWait(driver_item,15)
wait1 = ui.WebDriverWait(driver_detail,15)
#獲取URL和文章標題
def getURL_Title():
global SUMRESOURCES
##############################################################################
#需要鍵入想要獲取的信息,比如種類,排序方式,想看多少內容
##############################################################################
print "please select:"
kind=input("1-Hot\n2-Newest\n3-Classics\n4-Playable\n5-High Scores\n6-Wonderful but not popular\n7-Chinese film\n8-Hollywood\n9-Korea\n10-Japan\n11-Action movies\n12-Comedy\n13-Love story\n14-Science fiction\n15-Thriller\n16-Horror film\n17-Cartoon\nplease select:")
print "--------------------------------------------------------------------------"
sort=input("1-Sort by hot\n2-Sort by time\n3-Sort by score\nplease select:")
print "--------------------------------------------------------------------------"
number = input("TOP ?:")
print "--------------------------------------------------------------------------"
ask_long=input("don't need long-comments,enter 0,i like long-comments enter 1:")
print "--------------------------------------------------------------------------"
global save_name
save_name=raw_input("save_name (xx.txt):")
print "---------------------crawling...---------------------"
driver_item.get(url)
##############################################################################
#進行網(wǎng)頁get后,先進行電影種類選擇的模擬點擊操作,然后再是排序方式的選擇
#最后等待一會,元素都加載完了,才能開始爬電影,不然元素隱藏起來,不能被獲取
#wait.until是等待元素加載完成!
##############################################################################
wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind))
driver_item.find_element_by_xpath("http://div[@class='fliter-wp']/div/form/div/div/label[%s]"%kind).click()
wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort))
driver_item.find_element_by_xpath("http://div[@class='fliter-wp']/div/form/div[3]/div/label[%s]"%sort).click()
num=number+1#比如輸入想看的TOP22,那需要+1在進行操作,細節(jié)問題
time.sleep(2)
#打開幾次“加載更多”
num_time = num/20+1
wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='list-wp']/a[@class='more']"))
for times in range(1,num_time):
time.sleep(1)
driver_item.find_element_by_xpath("http://div[@class='list-wp']/a[@class='more']").click()
time.sleep(1)
wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='list']/a[%d]"%num))
#print '點擊\'加載更多\'一次'
#使用wait.until使元素全部加載好能定位之后再操作,相當于try/except再套個while把
for i in range(1,num):
wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='list']/a[%d]"%num))
list_title=driver_item.find_element_by_xpath("http://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的
#寫入txt中部分1
list_title_wr=list_title.text.encode('utf-8')#unicode碼,需要重新編碼再寫入txt
list_title_url_wr=list_title.get_attribute('href')
Write_txt('\n----------------------------------------------'+'NO' + str(SUMRESOURCES +1)+'----------------------------------------------','',save_name)
Write_txt(list_title_wr,list_title_url_wr,save_name)
SUMRESOURCES = SUMRESOURCES +1
try:#獲取具體內容和評論。href是每個超鏈接也就是資源單獨的url
getDetails(str(list_title.get_attribute('href')),ask_long)
except:
print 'can not get the details!'
##############################################################################
#當選擇一部電影后,進入這部電影的超鏈接,然后才能獲取
#同時別忽視元素加載的問題
#在加載長評論的時候,注意模擬點擊一次小三角,不然可能會使內容隱藏
##############################################################################
def getDetails(url,ask_long):
driver_detail.get(url)
wait1.until(lambda driver: driver.find_element_by_xpath("http://div[@id='link-report']/span"))
drama = driver_detail.find_element_by_xpath("http://div[@id='link-report']/span")
print u"劇情簡介:"+drama.text
drama_wr=drama.text.encode('utf-8')
Write_txt(drama_wr,'',save_name)
print "--------------------------------------------Hot comments TOP----------------------------------------------"
for i in range(1,5):#四個短評
try:
comments_hot = driver_detail.find_element_by_xpath("http://div[@id='hot-comments']/div[%s]/div/p"%i)
print u"最新熱評:"+comments_hot.text
comments_hot_wr=comments_hot.text.encode('utf-8')
Write_txt("--------------------------------------------Hot comments TOP%d----------------------------------------------"%i,'',save_name)
Write_txt(comments_hot_wr,'',save_name)
except:
print 'can not caught the comments!'
#加載長評
if ask_long==1:
try:
driver_detail.find_element_by_xpath("http://img[@class='bn-arrow']").click()
#wait.until(lambda driver: driver.find_element_by_xpath("http://div[@class='review-bd']/div[2]/div/div"))
time.sleep(1)
#解決加載長評會提示劇透問題導致無法加載
comments_get = driver_detail.find_element_by_xpath("http://div[@class='review-bd']/div[2]/div")
if comments_get.text.encode('utf-8')=='提示: 這篇影評可能有劇透':
comments_deep=driver_detail.find_element_by_xpath("http://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')
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 main():
getURL_Title()
driver_item.quit()
main()
另一個評論用了PhantomJS
實現(xiàn)效果
致謝
@mrlevo520-Python自定義豆瓣電影種類,排行,點評的爬取與存儲(初級)