Recently I want to create an App for movie information. Since Douban does not develop APIs externally, I am worried about how to input data. If you are learning Python recently, use it to capture movie information. Python2.7Mac OS
Capture the pages of the latest movies in movie heaven. Link: http://www.dytt8.net/html/gndy/dyzz/index.html
Get the link to the movie details page on the page
Import urllib2import osimport reimport string # movieUrls collection of movie URLs = [] # Get movie list def queryMovieList (): URL = 'HTTP: // movie conent = urllib2.urlopen (url) conent = conent. read () conent = conent. decode ('gb2312', 'ignore '). encode ('utf-8', 'ignore') pattern = re. compile ('. *?>
'+ '(.*?)', Re. S) items = re. findall (pattern, conent) str = ''. join (items) pattern = re. compile ('(.*?). *?(.*?)', Re. S) news = re. findall (pattern, str) for j in news: movieUrls. append ('HTTP: // www.dytt8.net' + j [0])
Capture movie data on the details page
Def queryMovieInfo (movieUrls): for index, item in enumerate (movieUrls): print ('movie URL: '+ item) conent = urllib2.urlopen (item) conent = conent. read () conent = conent. decode ('gb2312', 'ignore '). encode ('utf-8', 'ignore') movieName = re. findall (R'(.*?)
', Conent, re. s) if (len (movieName)> 0): movieName = movieName [0] + "" # capture name movieName = movieName [movieName. find ("") + 3: movieName. find ("")] else: movieName = "" print ("movie name:" + movieName. strip () movieContent = re. findall (R'(.*?)', Conent, re. S) pattern = re. compile ('
(.*?)', Re. s) movieDate = re. findall (pattern, movieContent [0]) if (len (movieDate)> 0): movieDate = movieDate [0]. strip () + ''else: movieDate =" "print (" movie Release Date: "+ movieDate [-10:]) pattern = re. compile ('
(.*?)
0): movieInfo = movieInfo [0] + ''# Delete
Label movieInfo = movieInfo. replace ("
"," ") # Split movieInfo = movieInfo according to the ◎ symbol. split ('◎') else: movieInfo = "" print ("basic movie information:") for item in movieInfo: print (item) # Movie poster pattern = re. compile ('', re. s) movieImg = re. findall (pattern, movieContent [0]) if (len (movieImg)> 0): movieImg = movieImg [0] else: movieImg = "" print ("movie poster: "+ movieImg) pattern = re. compile ('.*?', Re. s) movieDownUrl = re. findall (pattern, movieContent [0]) if (len (movieDownUrl)> 0): movieDownUrl = movieDownUrl [0] else: movieDownUrl = "" print ("movie: "+ movieDownUrl +" ") print (" ------------------------------------------------ \ n ")
Capture
If _ name __= = '_ main _': print ("starting to capture movie data"); queryMovieList () print (len (movieUrls) queryMovieInfo (movieUrls) print ("End capturing movie data ")
Summary
It is important to learn regular expressions !!!! Python syntax is good. Compared with Java...