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. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法2. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明4. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問(wèn)題8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. CSS hack用法案例詳解10. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
