Python Scrapy多頁(yè)數(shù)據(jù)爬取實(shí)現(xiàn)過(guò)程解析
1.先指定通用模板
url = ’https://www.qiushibaike.com/text/page/%d/’#通用的url模板pageNum = 1
2.對(duì)parse方法遞歸處理
parse第一次調(diào)用表示的是用來(lái)解析第一頁(yè)對(duì)應(yīng)頁(yè)面中的數(shù)據(jù)
對(duì)后面的頁(yè)碼的數(shù)據(jù)要進(jìn)行手動(dòng)發(fā)送
if self.pageNum <= 5: self.pageNum += 1 new_url = format(self.url%self.pageNum) #手動(dòng)請(qǐng)求(get)的發(fā)送 yield scrapy.Request(new_url,callback=self.parse)
完整示例
class QiubaiSpider(scrapy.Spider): name = ’qiubai’ # allowed_domains = [’www.xxx.com’] start_urls = [’https://www.qiushibaike.com/text/’] url = ’https://www.qiushibaike.com/text/page/%d/’#通用的url模板 pageNum = 1 #parse第一次調(diào)用表示的是用來(lái)解析第一頁(yè)對(duì)應(yīng)頁(yè)面中的段子內(nèi)容和作者 def parse(self, response): div_list = response.xpath(’//*[@id='content-left']/div’) all_data = [] for div in div_list: author = div.xpath(’./div[1]/a[2]/h2/text()’).extract_first() content = div.xpath(’./a[1]/div/span//text()’).extract() content = ’’.join(content) # 將解析的數(shù)據(jù)存儲(chǔ)到item對(duì)象 item = QiubaiproItem() item[’author’] = author item[’content’] = content # 將item提交給管道 yield item # item一定是提交給了優(yōu)先級(jí)最高的管道類 if self.pageNum <= 5: self.pageNum += 1 new_url = format(self.url%self.pageNum) #手動(dòng)請(qǐng)求(get)的發(fā)送 yield scrapy.Request(new_url,callback=self.parse)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python的文本常量與字符串模板之string庫(kù)2. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)3. 利用CSS制作3D動(dòng)畫4. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程5. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))6. Springboot 全局日期格式化處理的實(shí)現(xiàn)7. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法8. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題9. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼10. JAMon(Java Application Monitor)備忘記
