In general, the crawler framework crawl AJAX dynamic page is through some third-party WebKit library to manually execute the HTML page of the JS code, and finally the production of HTML code to the spider analysis. This article is through the use of Fiddler capture JSON data analysis Ajax page specific request content, find the interface URL to get the data, directly call the interface to obtain data, eliminating the trouble of introducing Python-webkit library, And because the data of the general AJAX request is structured data, this saves us the pain of parsing HTML with XPath.
Mobile phone Open embarrassing things encyclopedia app, using fiddler grab packet to get JSON data check the interface URL to get the normal access if you can access in a different browser to try
JSON data after Open is recommended with the Json-handle plugin (chrome install) Open
Code implementation: Take 100 pages as an example
items.py
1 Importscrapy2 3 4 classQiushibalkeitem (scrapy. Item):5 #Define the fields for your item here is like:6 #name = Scrapy. Field ()7Uid=Scrapy. Field ()8Nickname =Scrapy. Field ()9Gender=Scrapy. Field ()Ten Oneastrology=Scrapy. Field () A -Content=Scrapy. Field () -Crawl_time=scrapy. Field ()
View Code
spiders/qiushi.py
1 #-*-coding:utf-8-*-2 Importscrapy3 ImportJSON4 fromQiushibalke.itemsImportQiushibalkeitem5 fromDatetimeImportdatetime6 classQiushispider (scrapy. Spider):7Name ="Qiushi"8Allowed_domains = ["m2.qiushibaike.com"]9 defstart_requests (self):Ten forIinchRange (1,100): OneURL ="https://m2.qiushibaike.com/article/list/text?page={}". Format (i) A yieldScrapy. Request (url,callback=Self.parse_item) - - the defParse_item (Self, Response): -Datas = Json.loads (Response.text) ["Items"] - Print(datas) - forDatainchdatas: + #print (data[' votes ' [' Up ']) - #Print (data[' user ' [' uid ']) + #Print (data[' user ' ["Login"]) A #Print (data[' user ' ["gender"]) at #Print (data[' user ' ["Astrology"]) - -item =Qiushibalkeitem () -item["UID"]= data['User']["UID"] - -item["Nickname"] = data['User']["Login"] initem["Gender"] = data['User']["Gender"] - toitem["Astrology"] = data['User']["Astrology"] +item["content"]=data["content"] -item["Crawl_time"] =DateTime.Now () the * yieldItem $ View Code
pipelines.py
ImportPymysqlclassQiushibalkepipeline (object):defProcess_item (self, item, spider): Con= Pymysql.connect (host="127.0.0.1", user="Youusername", passwd="Youpassword", db="Qiushi", charset="UTF8") cur=con.cursor () SQL= ("INSERT INTO Baike (uid,nickname,gender,astrology,content,crawl_time)" "VALUES (%s,%s,%s,%s,%s,%s)") Lis= (item["UID"],item["Nickname"],item["Gender"],item["Astrology"],item["content"],item["Crawl_time"]) cur.execute (SQL, Lis) con.commit () Cur.close () con.close ()returnItem
settings.py
1Bot_name ='Qiushibalke'2 3Spider_modules = ['qiushibalke.spiders']4Newspider_module ='qiushibalke.spiders'5Robotstxt_obey =False6Download_delay = 57cookies_enabled =False8Default_request_headers = {9 "user-agent":"qiushibalke_10.13.0_wifi_auto_7",Ten #' Accept ': ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ', One #' accept-language ': ' en ', A } -Item_pipelines = { - 'Qiushibalke.pipelines.QiushibalkePipeline': 300, the #' scrapy_redis.pipelines.RedisPipeline ': -}View Code
Data
Scrapy 4 Crawl Ajax dynamic page (as an example of embarrassing encyclopedia app):