前言
之前的一篇文章已經(jīng)講過怎樣獲取鏈接,怎樣獲得參數(shù)了,詳情請(qǐng)看python爬取京東商城普通篇,本文將詳細(xì)介紹利用python爬蟲框架scrapy如何爬取京東商城,下面話不多說了,來看看詳細(xì)的介紹吧。
代碼詳解
1、首先應(yīng)該構(gòu)造請(qǐng)求,這里使用scrapy.Request,這個(gè)方法默認(rèn)調(diào)用的是start_urls構(gòu)造請(qǐng)求,如果要改變默認(rèn)的請(qǐng)求,那么必須重載該方法,這個(gè)方法的返回值必須是一個(gè)可迭代的對(duì)象,一般是用yield返回。
代碼如下:
def start_requests(self): for i in range(1,101): page=i*2-1 #這里是構(gòu)造請(qǐng)求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)頁。這里還是和上面說的一樣,這個(gè)url得到的僅僅是前一半的信息,如果想要得到后一半的信息還有再次請(qǐng)求,這里還有注意的就是一個(gè)技巧:一般先解析出一個(gè)數(shù)據(jù)的數(shù)組,不急著取出第一個(gè)數(shù),先要用if語句判斷,因?yàn)槿绻玫降氖荹],那么直接取出[0]是會(huì)報(bào)錯(cuò)的,這只是一個(gè)避免報(bào)錯(cuò)的方法吧。
代碼如下:
def parse_url(self,response): if response.status==200: #判斷是否請(qǐng)求成功 # print response.url pids = set() #這個(gè)集合用于過濾和保存得到的id,用于作為后面的ajax請(qǐng)求的url構(gòu)成 try: all_goods = response.xpath("http://div[@id='J_goodsList']/ul/li") #首先得到所有衣服的整個(gè)框架,然后從中抽取每一個(gè)框架 for goods in all_goods: #從中解析每一個(gè) # scrapy.shell.inspect_response(response,self) #這是一個(gè)調(diào)試的方法,這里會(huì)直接打開調(diào)試模式 items = JdSpiderItem() #定義要抓取的數(shù)據(jù) img_url_src = goods.xpath("div/div[1]/a/img/@src").extract() # 如果不存在就是一個(gè)空數(shù)組[],因此不能在這里取[0] img_url_delay = goods.xpath( "div/div[1]/a/img/@data-lazy-img").extract() # 這個(gè)是沒有加載出來的圖片,這里不能寫上數(shù)組取第一個(gè)[0] price = goods.xpath("div/div[3]/strong/i/text()").extract() #價(jià)格 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: # 如果到了沒有加載完成的圖片,就取這個(gè)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寫在這里就是每解析一個(gè)鍵褲子就會(huì)調(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) #再次請(qǐng)求,這里是請(qǐng)求ajax加載的數(shù)據(jù),必須放在這里,因?yàn)橹挥械鹊降玫剿械膒id才能構(gòu)成這個(gè)請(qǐng)求,回調(diào)函數(shù)用于下面的解析2、從上面代碼的最后可以看出最后就是解析ajax加載的網(wǎng)頁了,這里調(diào)用的next_half_parse函數(shù),和解析前面一個(gè)網(wǎng)頁一樣,這里需要的注意的是,如果前面定義的數(shù)據(jù)沒有搜索完畢是不能使用yield items的,必須將items通過meta傳入下一個(gè)回調(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è)置請(qǐng)求池,mysql存儲(chǔ),沒有使用到ip代理,這個(gè)在我前面的博客中又講到,這里就不再贅述了。
想看源代碼的朋友請(qǐng)
點(diǎn)擊這里 或者 本地下載
小技巧
JOBDIR=file_name,這里的file_name是一個(gè)文件的名字DOWNLOAD_DELAY = 2:設(shè)置每一次的間隔時(shí)間 RANDOMIZE_DOWNLOAD_DELAY = True:這個(gè)是隨機(jī)設(shè)置延遲時(shí)間 在設(shè)置的時(shí)間的0.5-1.5倍之間,這樣可以更有效的防止被ban,一般是配套使用的ROBOTSTXT_OBEY = False :這里是表示不遵循robots.txt文件,默認(rèn)是True表示遵循,這里將之改成FalseCONCURRENT_REQUESTS :設(shè)置最大請(qǐng)求數(shù),這里默認(rèn)的時(shí)16,我們可以根據(jù)自己電腦的配置改的大一點(diǎn)來加快請(qǐng)求的速度總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選