福州网站建设咨询,数字化展厅设计方案,WordPress自动发英文文章,如何做地方网站目标 在实际开发过程中#xff0c;我们所需要的数据往往需要通过多个页面的数据汇总得到#xff0c;通过列表获取到的数据只有简单的介绍。站在Scrapy框架的角度来看#xff0c;实际上就是考虑如何处理一个item包含多级页面数据的问题。本文将以获取叶子猪网站的手游排行榜及…目标 在实际开发过程中我们所需要的数据往往需要通过多个页面的数据汇总得到通过列表获取到的数据只有简单的介绍。站在Scrapy框架的角度来看实际上就是考虑如何处理一个item包含多级页面数据的问题。本文将以获取叶子猪网站的手游排行榜及手游详情为学习案例来解决这个问题。 版本 Scrapy 2.12.0 实战
第一步搭建Scrapy框架。略过如果不会搭建的通过可以看我之前的Scrapy入门文章。
第二步通过打开目标网页查看网页代码我们可以的到手游排行榜的基础信息这里我们只获取标题。
import scrapyclass SytopSpider(scrapy.Spider):name sytopallowed_domains [sy.yzz.cn]start_urls [http://sy.yzz.cn/news/14324-1.shtml]def parse(self, response):a_list response.xpath(//ul[classitem-pt-list]/li/div[1]/a)for a in a_list:# 标题alt a.xpath(./img/alt).get()print(alt) 第三步进入二级页面获取描述信息。此时日志打印可以看到单个游戏的信息并没有组合起来。
import scrapyclass SytopSpider(scrapy.Spider):name sytopallowed_domains [sy.yzz.cn]start_urls [http://sy.yzz.cn/news/14324-1.shtml]def parse(self, response):a_list response.xpath(//ul[classitem-pt-list]/li/div[1]/a)for a in a_list:# 标题alt a.xpath(./img/alt).get()print(alt)# 二级页面的urlinfo_url a.xpath(./href).get()print(f二级页面的url是{info_url})meta {alt: alt}yield scrapy.Request(urlinfo_url, callbackself.parse_info)def parse_info(self, response):p_list response.xpath(//div[classcontent]//p)for p in p_list:contentp.xpath(string(.)).get()print(content)
第四步组合item数据。scrapy.Request方法中的meta参数很重要它实现了深度爬取。比如在爬取多层级页面时使用 meta 参数传递父页面的信息到子页面。
import scrapyfrom yezizhu.items import YezizhuItemclass SytopSpider(scrapy.Spider):name sytopallowed_domains [sy.yzz.cn]start_urls [http://sy.yzz.cn/news/14324-1.shtml]def parse(self, response):a_list response.xpath(//ul[classitem-pt-list]/li/div[1]/a)for a in a_list:# 标题alt a.xpath(./img/alt).get()# 二级页面的urlinfo_url a.xpath(./href).get()meta {alt: alt}yield scrapy.Request(urlinfo_url, callbackself.parse_info,metameta)def parse_info(self, response):p_list response.xpath(//div[classcontent]//p)print(start)alt response.meta[alt]print(alt)contentfor p in p_list:contentcontent\np.xpath(string(.)).get()print(content) 第五步创建item属性。
class YezizhuItem(scrapy.Item):alt scrapy.Field()content scrapy.Field()
第六步传递item属性值并将item对象传递给管道。
import scrapyfrom yezizhu.items import YezizhuItemclass SytopSpider(scrapy.Spider):name sytopallowed_domains [sy.yzz.cn]start_urls [http://sy.yzz.cn/news/14324-1.shtml]def parse(self, response):a_list response.xpath(//ul[classitem-pt-list]/li/div[1]/a)for a in a_list:# 标题alt a.xpath(./img/alt).get()# 二级页面的urlinfo_url a.xpath(./href).get()meta {alt: alt}yield scrapy.Request(urlinfo_url, callbackself.parse_info,metameta)def parse_info(self, response):p_list response.xpath(//div[classcontent]//p)print(start)alt response.meta[alt]print(alt)contentfor p in p_list:contentcontent\np.xpath(string(.)).get()print(content)top_contentYezizhuItem(altalt, contentcontent)yield top_content
第七步在settings.py文件中开启管道。
ITEM_PIPELINES {yezizhu.pipelines.YezizhuPipeline: 300,
}
第八步在管道中设置下载数据并启动项目。
import jsonclass YezizhuPipeline:# 在爬虫文件开始之前就执行的方法def open_spider(self, spider):self.fp open(C:\\Users\\Administrator\\Desktop\\test\\a.json, w, encodingutf-8)self.fp.write([)def process_item(self, item, spider):line json.dumps(dict(item), ensure_asciiFalse) ,\nself.fp.write(line)return item# 在爬虫文件执行之后再执行的方法def close_spider(self, spider):# 删除最后一个多余的逗号并关闭 JSON 数组self.fp.seek(self.fp.tell() - 3, 0)self.fp.write(\n])self.fp.close()