Capture links and comments of various types of Douban movies, and sort the code by rating as follows:
Import urllib. request
Import re
Import time
Def movie (movieTag ):
TagUrl = urllib. request. urlopen (url)
TagUrl_read = tagUrl. read (). decode ('utf-8 ')
Return tagUrl_read
Def subject (tagUrl_read ):
'''
There are still problems:
① This sorting is only for a single page, but not for movies on all pages
② Add a movie link in the next update to consider adding a movie poster
③ List to be appended
④ Import to local txt or excel
⑤ Can a movie name be matched with an array of links and names, scores, and comments?
⑥
'''
# Regular expressions match movie names (links), ratings, and comments
NameURL = re. findall (r' (http://movie.douban.com/subject/[0-9.]+) \/"\ s + title =" (. +) "', tagUrl_read)
ScoreURL = re. findall (r' ([0-9.] +) <\/span> ', tagUrl_read)
EvaluateURL = re. findall (r' \ (\ w +) rating \) <\/span> ', tagUrl_read)
MovieLists = list (zip (nameURL, scoreURL, evaluateURL ))
Newlist. extend (movieLists)
Return newlist
# Use quote to process Special (Chinese) characters
Movie_type = urllib. request. quote (input ('Enter the movie type (such as plot, comedy, suspense ):'))
Page_end = int (input ('Enter the page number at the end of the search :'))
Num_end = page_end * 20
Num = 0
Page_num = 1
Newlist = []
While num Url = r 'http: // movie.douban.com/tag/s s? Start = % d' % (movie_type, num)
Movie_url = movie (url)
Subject_url = subject (movie_url)
Num = page_num * 20
Page_num + = 1
Else:
# Use the sorted function to sort the list. if the reverse parameter is True, the list is in ascending order. if it is default or False, the list is in descending order. key = lambda does not quite understand the principle here.
MovieLIST = sorted (newlist, key = lambda movieList: movieList [1], reverse = True)
For movie in movieLIST:
Print (movie)
Time. sleep (3)
Print ('terminal ')