1. A simple crawler: Crawl the information of popular movies of watercress
Skills: Get Web page source code, regular expression, function call, global variable definition
1 #!/usr/bin/env python2 #-*-coding=utf-8-*-3 ImportRequests4 ImportJSON5 ImportRe6 ImportSYS7 Reload (SYS)8Sys.setdefaultencoding ("Utf-8")9ClassInfo = []Tenf = open ('Info.txt','W') One Anum =0 - defwrite (HTM): -TITL = Re.findall ('data-tit (. *?) Data-enough', Htm.text,re. S) the foreachinchtitl: - #Print each -info = {} - #Print each +info['title'] = Re.search ('le= "(. *?)"', Each,re. S). Group (1) -info[' Year'] = Re.search ('data-release= "(. *?)" Data', Each,re. S). Group (1) +info['Rating']= Re.findall ('data-rate= "(. *?)" Data-star', Each,re. S) [0] Ainfo[' Time'] = Re.findall ('data-duration= "(. *?)" Data-re', Each,re. S) [0] atinfo['Reg'] = Re.findall ('data-region= "(. *?)" Data-dir', Each,re. S) [0] -info['Act'] = Re.findall ('data-actors= "(. *?)" Data-in', Each,re. S) [0] - GlobalNum #全局的定义 -num = num + 1 -F.writelines ('%d\n'%num) -F.writelines (U'Movie Name:'+info['title'] +'\ n') inF.writelines (U'Starring:'+info['Act'] +'\ n') -F.writelines (U'Movie Area:'+ info['Reg']+'\ n') toF.writelines (U'release Year:'+ info[' Year']+'\ n') +F.writelines (U'Movie Duration:'+ info[' Time']+'\ n') -F.writelines (U'Score:'+ info['Rating']+'\ n') the defgetremen (): * #html = requests.get (' http://movie.douban.com/') $URL ='http://movie.douban.com/'Panax NotoginsengHTML =requests.get (URL) -Html.encoding ='Utf-8' the #Print Html.text + Write (HTML) A if __name__=="__main__": theGetremen ()
Python a simple reptile (1)