Python regular expression matching captures Douban movie links and comments code sharing

Source: Internet
Author: User
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 ')

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.