SCrapy爬蟲大戰(zhàn)京東商城
引言
上一篇已經(jīng)講過怎樣獲取鏈接,怎樣獲得參數(shù)了祝闻,詳情請(qǐng)看python爬取京東商城普通篇
代碼詳解
- 首先應(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)槿绻玫降氖?code>[]杭煎,那么直接取出[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ù)用于下面的解析
- 從上面代碼的最后可以看出最后就是解析
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 "**************************************************"
- 當(dāng)然這里還用到了設(shè)置請(qǐng)求池节猿,
mysql
存儲(chǔ),沒有使用到ip
代理漫雕,這個(gè)在我前面的博客中又講到滨嘱,這里就不再贅述了,想看源代碼的朋友請(qǐng)點(diǎn)擊這里
小技巧
- 人們會(huì)抱怨為什么自己的爬蟲在中途斷開就要重頭開始爬浸间,為什么不能從斷開那里開始爬呢太雨,這里提供一個(gè)方法:在配置文件
settings.py
中加入JOBDIR=file_name
,這里的file_name
是一個(gè)文件的名字
- 設(shè)置下載延遲防止被
ban
: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
表示遵循囊扳,這里將之改成False
CONCURRENT_REQUESTS
:設(shè)置最大請(qǐng)求數(shù)吩翻,這里默認(rèn)的時(shí)16
,我們可以根據(jù)自己電腦的配置改的大一點(diǎn)來加快請(qǐng)求的速度