Copy codeThe Code is 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' <span \ s + class = "rating_nums"> ([0-9.] +) <\/span> ', tagUrl_read)
EvaluateURL = re. findall (R' <span \ s + class = "pl"> \ (\ w +) Comments \) <\/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 <num_end:
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 ')