Or a cat's eye movie, for example, this time using Pyquery library to crawl
1. Simple demo, see How to extract information using pyquery, and combine the extracted data
#Coding:utf-8#AUTHOR:HMKImportRequests fromPyqueryImportPyquery as Pqurl='HTTP://MAOYAN.COM/BOARD/4'Header= {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "accept-encoding":"gzip, deflate, SDCH", "Accept-language":"zh-cn,zh;q=0.8", "Cache-control":"max-age=0", "Connection":"keep-alive", "Host":"maoyan.com", "Referer":"Http://maoyan.com/board", "upgrade-insecure-requests":"1", "user-agent":"mozilla/5.0 (Windows NT 6.1; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.75 safari/537.36"}r= Requests.get (URL, headers=header) R.encoding=r.apparent_encodinghtml=R.textPrint(type (HTML)) Doc=PQ (HTML)#Print ((Doc (' DD '). Find ('. Board-index ' )))#Print (Doc ('. Name '). Text ())#Print (Doc ('. Releasetime '). Text ())#Print (Doc (' DD '). Find ('. Integer '). Text () +doc ('. Fraction '). Text ())List = [] forTinchDoc'DD'): Index= PQ (t). Find ('. Board-index'). Text ()Print(index) Movie= PQ (t). Find ('. Name'). Text ()Print(movie) time= PQ (t). Find ('. Releasetime'). Text ()Print(time) score= PQ (t). Find ('. Integer'). Text () + PQ (t). Find ('. Fraction'). Text ()Print(Score) list.append ([Index, movie, time, score])Print(list)
2. Official code
#Coding:utf-8#AUTHOR:HMKImportRequests fromPyqueryImportPyquery as PQImportpymysql.cursorsdefget_html (URL, header):Try: R= Requests.get (Url=url, headers=header) R.encoding=r.apparent_encodingreturnR.textexcept: returnNonedefget_data (HTML, list_data): Doc=PQ (HTML) forTinchDoc'DD'): Index= PQ (t). Find ('. Board-index'). Text ()Print(index) Movie= PQ (t). Find ('. Name'). Text ()Print(movie) time= PQ (t). Find ('. Releasetime'). Text ()Print(time) score= PQ (t). Find ('. Integer'). Text () + PQ (t). Find ('. Fraction'). Text ()Print(Score) list_data.append ([Index, movie, time, score])defwrite_sql (data): Conn= Pymysql.connect (host='localhost', the user='Root', Password='123456', DB='Test', CharSet='UTF8') cur=conn.cursor () forIinchData:"""The data parameter here is a list that matches and processes (is a large list that contains all the movie information, each of which has its own list; iterate over a large list to extract each set of movie information so that each group of movie information extracted is a small list, Then you can write each set of movie information to the database."""movie= I#For each set of movie information, this can be seen as a set of movie data to be inserted into the databasesql ="INSERT INTO Maoyan_movie (Ranking,movie,release_time,score) values (%s,%s,%s,%s)" #SQL INSERT Statement Try: Cur.execute (sql, Movie)#execute the SQL statement, which refers to the data to be inserted into the databaseConn.commit ()#do not forget to submit the action after the insert is complete Print('Import succeeded') except: Print('Import Failed') Cur.close ()#Close CursorsConn.close ()#Close ConnectiondefMain (): Start_url='HTTP://MAOYAN.COM/BOARD/4'Depth= 10#Crawl Depth (page flip)Header = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "accept-encoding":"gzip, deflate, SDCH", "Accept-language":"zh-cn,zh;q=0.8", "Cache-control":"max-age=0", "Connection":"keep-alive", "Host":"maoyan.com", "Referer":"Http://maoyan.com/board", "upgrade-insecure-requests":"1", "user-agent":"mozilla/5.0 (Windows NT 6.1; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.75 safari/537.36"} forIinchRange (depth): URL= Start_url +'? offset='+ STR (10 *i) HTML=get_html (URL, header) List_data=[] get_data (HTML, List_data) Write_sql (list_data)#print (list_data)if __name__=="__main__": Main ()
In fact, in this case, using Pyquery to extract information is the simplest and easiest, directly using the CSS selector can get the data you want to
Cat's Eye Movie Crawl (iii): Requests+pyquery, and store data in MySQL database