python爬蟲框架scrapy實戰(zhàn)之爬取京東商城進(jìn)階篇
前言
之前的一篇文章已經(jīng)講過怎樣獲取鏈接,怎樣獲得參數(shù)了,詳情請看python爬取京東商城普通篇,本文將詳細(xì)介紹利用python爬蟲框架scrapy如何爬取京東商城,下面話不多說了,來看看詳細(xì)的介紹吧。
代碼詳解
1、首先應(yīng)該構(gòu)造請求,這里使用scrapy.Request,這個方法默認(rèn)調(diào)用的是start_urls構(gòu)造請求,如果要改變默認(rèn)的請求,那么必須重載該方法,這個方法的返回值必須是一個可迭代的對象,一般是用yield返回。
代碼如下:
def start_requests(self): for i in range(1,101): page=i*2-1 #這里是構(gòu)造請求url的page,表示奇數(shù) url=self.start_url+str(page) yield scrapy.Request(url,meta={'search_page':page+1},callback=self.parse_url) #這里使用meta想回調(diào)函數(shù)傳入數(shù)據(jù),回調(diào)函數(shù)使用response.meta['search-page']接受數(shù)據(jù)
下面就是解析網(wǎng)頁了,從上面看出這里的解析回調(diào)函數(shù)是parse_url,因此在此函數(shù)中解析網(wǎng)頁。這里還是和上面說的一樣,這個url得到的僅僅是前一半的信息,如果想要得到后一半的信息還有再次請求,這里還有注意的就是一個技巧:一般先解析出一個數(shù)據(jù)的數(shù)組,不急著取出第一個數(shù),先要用if語句判斷,因為如果得到的是[],那么直接取出[0]是會報錯的,這只是一個避免報錯的方法吧。
代碼如下:
def parse_url(self,response): if response.status==200: #判斷是否請求成功 # print response.url pids = set() #這個集合用于過濾和保存得到的id,用于作為后面的ajax請求的url構(gòu)成 try: all_goods = response.xpath("http://div[@id='J_goodsList']/ul/li") #首先得到所有衣服的整個框架,然后從中抽取每一個框架 for goods in all_goods: #從中解析每一個 # scrapy.shell.inspect_response(response,self) #這是一個調(diào)試的方法,這里會直接打開調(diào)試模式 items = JdSpiderItem() #定義要抓取的數(shù)據(jù) img_url_src = goods.xpath("div/div[1]/a/img/@src").extract() # 如果不存在就是一個空數(shù)組[],因此不能在這里取[0] img_url_delay = goods.xpath( "div/div[1]/a/img/@data-lazy-img").extract() # 這個是沒有加載出來的圖片,這里不能寫上數(shù)組取第一個[0] price = goods.xpath("div/div[3]/strong/i/text()").extract() #價格 cloths_name = goods.xpath("div/div[4]/a/em/text()").extract() shop_id = goods.xpath("div/div[7]/@ data-shopid").extract() cloths_url = goods.xpath("div/div[1]/a/@href").extract() person_number = goods.xpath("div/div[5]/strong/a/text()").extract() pid = goods.xpath("@data-pid").extract() # product_id=goods.xpath("@data-sku").extract() if pid: pids.add(pid[0]) if img_url_src: # 如果img_url_src存在 print img_url_src[0] items['img_url'] = img_url_src[0] if img_url_delay: # 如果到了沒有加載完成的圖片,就取這個url print img_url_delay[0] items['img_url'] = img_url_delay[0] # 這里如果數(shù)組不是空的,就能寫了 if price: items['price'] = price[0] if cloths_name: items['cloths_name'] = cloths_name[0] if shop_id: items['shop_id'] = shop_id[0] shop_url = "https://mall.jd.com/index-" + str(shop_id[0]) + ".html" items['shop_url'] = shop_url if cloths_url: items['cloths_url'] = cloths_url[0] if person_number: items['person_number'] = person_number[0] # if product_id: # print "************************************csdjkvjfskvnk***********************" # print self.comments_url.format(str(product_id[0]),str(self.count)) # yield scrapy.Request(url=self.comments_url.format(str(product_id[0]),str(self.count)),callback=self.comments) #yield scrapy.Request寫在這里就是每解析一個鍵褲子就會調(diào)用回調(diào)函數(shù)一次 yield items except Exception: print "********************************************ERROR**********************************************************************" yield scrapy.Request(url=self.search_url.format(str(response.meta['search_page']),",".join(pids)),callback=self.next_half_parse) #再次請求,這里是請求ajax加載的數(shù)據(jù),必須放在這里,因為只有等到得到所有的pid才能構(gòu)成這個請求,回調(diào)函數(shù)用于下面的解析
2、從上面代碼的最后可以看出最后就是解析ajax加載的網(wǎng)頁了,這里調(diào)用的next_half_parse函數(shù),和解析前面一個網(wǎng)頁一樣,這里需要的注意的是,如果前面定義的數(shù)據(jù)沒有搜索完畢是不能使用yield items
的,必須將items通過meta傳入下一個回調(diào)函數(shù)繼續(xù)完善后才能yield items
,這里就不需要了。
代碼如下:
#分析異步加載的網(wǎng)頁 def next_half_parse(self,response): if response.status==200: print response.url items=JdSpiderItem() #scrapy.shell.inspect_response(response,self) #y用來調(diào)試的 try: lis=response.xpath("http://li[@class='gl-item']") for li in lis: cloths_url=li.xpath("div/div[1]/a/@href").extract() img_url_1=li.xpath("div/div[1]/a/img/@src").extract() img_url_2=li.xpath("div/div[1]/a/img/@data-lazy-img").extract() cloths_name=li.xpath("div/div[4]/a/em/text()").extract() price=li.xpath("div/div[3]/strong/i/text()").extract() shop_id=li.xpath("div/div[7]/@data-shopid").extract() person_number=li.xpath("div/div[5]/strong/a/text()").extract() if cloths_url: print cloths_url[0] items['cloths_url']=cloths_url[0] if img_url_1: print img_url_1[0] items['img_url']=img_url_1 if img_url_2: print img_url_2[0] items['img_url']=img_url_2[0] if cloths_name: items['cloths_name']=cloths_name[0] if price: items['price']=price[0] if shop_id: items['shop_id']=shop_id[0] items['shop_url']="https://mall.jd.com/index-" + str(shop_id[0]) + ".html" if person_number: items['person_number']=person_number[0] yield items #又一次的生成,這里是完整的數(shù)據(jù),因此可以yield items except Exception: print "**************************************************"
3、當(dāng)然這里還用到了設(shè)置請求池,mysql存儲,沒有使用到ip代理,這個在我前面的博客中又講到,這里就不再贅述了。
想看源代碼的朋友請
點(diǎn)擊這里 或者 本地下載
小技巧
- 人們會抱怨為什么自己的爬蟲在中途斷開就要重頭開始爬,為什么不能從斷開那里開始爬呢,這里提供一個方法:在配置文件settings.py中加入
JOBDIR=file_name
,這里的file_name是一個文件的名字 - 設(shè)置下載延遲防止被ban:
DOWNLOAD_DELAY = 2
:設(shè)置每一次的間隔時間RANDOMIZE_DOWNLOAD_DELAY = True
:這個是隨機(jī)設(shè)置延遲時間 在設(shè)置的時間的0.5-1.5倍之間,這樣可以更有效的防止被ban,一般是配套使用的 ROBOTSTXT_OBEY = False
:這里是表示不遵循robots.txt文件,默認(rèn)是True表示遵循,這里將之改成FalseCONCURRENT_REQUESTS
:設(shè)置最大請求數(shù),這里默認(rèn)的時16,我們可以根據(jù)自己電腦的配置改的大一點(diǎn)來加快請求的速度
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Python爬蟲框架Scrapy安裝使用步驟
- 零基礎(chǔ)寫python爬蟲之使用Scrapy框架編寫爬蟲
- 使用scrapy實現(xiàn)爬網(wǎng)站例子和實現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟
- scrapy爬蟲完整實例
- 深入剖析Python的爬蟲框架Scrapy的結(jié)構(gòu)與運(yùn)作流程
- 講解Python的Scrapy爬蟲框架使用代理進(jìn)行采集的方法
- Python使用Scrapy爬蟲框架全站爬取圖片并保存本地的實現(xiàn)代碼
- Python的Scrapy爬蟲框架簡單學(xué)習(xí)筆記
- 實踐Python的爬蟲框架Scrapy來抓取豆瓣電影TOP250
- 使用Python的Scrapy框架編寫web爬蟲的簡單示例
- 淺析python實現(xiàn)scrapy定時執(zhí)行爬蟲
- Scrapy爬蟲多線程導(dǎo)致抓取錯亂的問題解決
相關(guān)文章
Django實現(xiàn)靜態(tài)文件緩存到云服務(wù)的操作方法
這篇文章主要介紹了Django實現(xiàn)靜態(tài)文件緩存到云服務(wù)的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08python中函數(shù)返回多個結(jié)果的實例方法
在本篇文章里小編給大家整理了一篇關(guān)于python中函數(shù)返回多個結(jié)果的實例方法,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12Python數(shù)據(jù)分析matplotlib折線圖案例處理
這篇文章主要介紹了Python數(shù)據(jù)分析matplotlib折線圖案例處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08