当前位置: 首页 > news >正文

洮南网站软文拟发布的平台与板块

洮南网站,软文拟发布的平台与板块,网站建设及维护费用,wordpress教育主题目标 在实际开发过程中,我们所需要的数据往往需要通过多个页面的数据汇总得到,通过列表获取到的数据只有简单的介绍。站在Scrapy框架的角度来看,实际上就是考虑如何处理一个item包含多级页面数据的问题。本文将以获取叶子猪网站的手游排行榜及…

目标

        在实际开发过程中,我们所需要的数据往往需要通过多个页面的数据汇总得到,通过列表获取到的数据只有简单的介绍。站在Scrapy框架的角度来看,实际上就是考虑如何处理一个item包含多级页面数据的问题。本文将以获取叶子猪网站的手游排行榜及手游详情为学习案例来解决这个问题。


版本

        Scrapy 2.12.0


实战

第一步:搭建Scrapy框架。略过,如果不会搭建的通过可以看我之前的Scrapy入门文章。

第二步:通过打开目标网页,查看网页代码,我们可以的到手游排行榜的基础信息,这里我们只获取标题。

import scrapyclass SytopSpider(scrapy.Spider):name = "sytop"allowed_domains = ["sy.yzz.cn"]start_urls = ["http://sy.yzz.cn/news/14324-1.shtml"]def parse(self, response):a_list = response.xpath("//ul[@class='item-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 = "sytop"allowed_domains = ["sy.yzz.cn"]start_urls = ["http://sy.yzz.cn/news/14324-1.shtml"]def parse(self, response):a_list = response.xpath("//ul[@class='item-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(url=info_url, callback=self.parse_info)def parse_info(self, response):p_list = response.xpath("//div[@class='content']//p")for p in p_list:content=p.xpath("string(.)").get()print(content)

第四步:组合item数据。scrapy.Request方法中的meta参数很重要,它实现了深度爬取。比如:在爬取多层级页面时,使用 meta 参数传递父页面的信息到子页面。

import scrapyfrom yezizhu.items import YezizhuItemclass SytopSpider(scrapy.Spider):name = "sytop"allowed_domains = ["sy.yzz.cn"]start_urls = ["http://sy.yzz.cn/news/14324-1.shtml"]def parse(self, response):a_list = response.xpath("//ul[@class='item-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(url=info_url, callback=self.parse_info,meta=meta)def parse_info(self, response):p_list = response.xpath("//div[@class='content']//p")print("==============start================")alt = response.meta["alt"]print(alt)content=""for p in p_list:content=content+"\n"+p.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 = "sytop"allowed_domains = ["sy.yzz.cn"]start_urls = ["http://sy.yzz.cn/news/14324-1.shtml"]def parse(self, response):a_list = response.xpath("//ul[@class='item-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(url=info_url, callback=self.parse_info,meta=meta)def parse_info(self, response):p_list = response.xpath("//div[@class='content']//p")print("==============start================")alt = response.meta["alt"]print(alt)content=""for p in p_list:content=content+"\n"+p.xpath("string(.)").get()print(content)top_content=YezizhuItem(alt=alt, content=content)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", encoding="utf-8")self.fp.write("[")def process_item(self, item, spider):line = json.dumps(dict(item), ensure_ascii=False) + ",\n"self.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()

http://www.mnyf.cn/news/44150.html

相关文章:

  • 网站商城制作平台推广方式有哪些
  • 龙岗网络营销网站制作哪里好优化大师安卓版
  • 余姚网站建设yyshj潍坊seo计费
  • 新手如何建设网站网页广告调词平台多少钱
  • 个人网站备案名称 技巧网站优化seo培
  • 怎么用动态网站模板百度点击排名收费软件
  • 海城seo网站排名优化推广企业网站seo排名优化
  • 永州网站制作市场调研报告
  • 武汉网站建设哪家好搜索引擎优化的例子
  • 建网站可靠多用户建站平台
  • 里水网站建设seo优化服务是什么意思
  • qq介绍网站做兼职是真的吗商品推广软文范例200字
  • 做网站 你的出路在哪里搜索引擎营销简称seo
  • 郑州做网站优化电话seo引擎优化软件
  • 哪些网站是用jsp做的百度网站打不开
  • 美国做ppt的网站有哪些seo综合查询怎么用
  • 哈尔滨网站托管网上推广的平台有哪些
  • 源码网站git新闻头条最新消息10条
  • 自己做的免费的网站天天重发好吗模板建站哪里有
  • 政府网站建设 服务群众各个广告联盟的标识
  • 做app网站的软件有哪些内容吗青岛网络推广公司
  • 投资好项目南宁市优化网站公司
  • 勒流有做网站的吗个人怎么做免费百度推广
  • 建设银行官方网站官网营销网站建设免费
  • 线上培训网站开发有网站模板怎么建站
  • 百度翻译api wordpress天津百度seo排名优化软件
  • 网站开发毕业设计答辩优化大师的三大功能
  • 做网站 公司互联网营销策划
  • 伊春网站建设百度客服电话
  • wordpress网站标题自定义免费seo网站的工具