Use Python+selenium to crawl the top 12 movies in the Watercress movie and sort them by their scores aggressively

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.