Crawl the top 12 movies in the Watercress Movie (http://movie.douban.com/nowplaying/chengdu/), and sort by rating, save to TXT file
1 #Coding=utf-82 fromSeleniumImportWebdriver3 ImportUnitTest4 fromTimeImportSleep5 6 classDoubanmovie (unittest. TestCase):7 8 defsetUp (self):9Self.dr =Webdriver. Chrome ()TenSelf.top_movie_list =SELF.GET_DOUBAN_MOVIES_TOP12 () OneSelf.movie =Self.get_movie_rank_file () A - defget_douban_movies_top12 (self): - " "get the top 12 movie names and ratings from the Watercress film in Chengdu" " theSelf.dr.get ("https://movie.douban.com/nowplaying/chengdu/") -Sleep (3) -Movie_list = [] -i =0 + whileI < 60: -Movie_name = Self.dr.find_elements_by_css_selector ('. Lists Li') [I].get_attribute ('Data-title')#Locate the movie name +Movie_grand = Self.dr.find_elements_by_css_selector ('. Lists Li') [I].get_attribute ('Data-score')#Positioning movie Ratings A movie_list.append ([Movie_name,movie_grand]) ati + = 5 -Movie_list.sort (key=LambdaY:float (Y[1]), reverse=true)#sort the movies you get by using sort according to the movie score high to low - returnmovie_list - - defGet_movie_rank_file (self): -Self.file_title ='the top 12 films in Chengdu, the watercress film' inSelf.file = open (Self.file_title +'. txt','WB') - forIteminchself.top_movie_list: toSelf.file.write (('Movie Name:'+ item[0] +' '+'Movie Rating:'+ item[1] +'\ n'). Encode ('Utf-8')) + self.file.close () - the defTest_movie (self): * Pass $ Print("Get finished")Panax Notoginseng - defTearDown (self): the self.dr.quit () + A if __name__=='__main__': theUnittest.main ()
Note: There is no rating in the movie for 0 points.
Use Python+selenium to crawl the top 12 movies in the Watercress movie and sort them by their scores aggressively