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

.net网站项目有哪些购物网站开发语言

.net网站项目有哪些,购物网站开发语言,做断桥铝门窗网站,保定百度关键词优化目标 在实际开发过程中#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()
http://www.hkea.cn/news/14444227/

相关文章:

  • 我自己做的网站一直没有效果怎么办域名过期了被别人拿去做违法
  • 在电脑上怎么做网站php网站开发是做什么的
  • 12306网站做的好丑北京网站设计公司哪家公司好
  • 手机怎么做自己的网站食品网站建设书
  • 南宁网站建设策划外包wordpress 百度广告
  • 免费下载网站模版php项目开发案例源码
  • 深圳网站关键词优化排名西安企业建站公司
  • 有没有女的做任务的网站app扁平化设计网站
  • 广西省建设厅网站网站被重定向跳转
  • 海珠区做网站的公司上海网站建设网站游戏
  • 网上怎么自己做网站360doc 网站怎么做
  • 物流企业网站淄博公司做网站
  • 如何做一款服装网站ftp网站 免费
  • 旅游哪个网站最好静态网站开发预期效果
  • 网站搬家后出错网站建设从哪入手
  • 芷江建设工程招投标网站公司策划方案怎么做
  • 建设一个简单的网站精美旅游网站案例
  • 公司名字大全霸气南宁网站优化推广方案
  • 网站建设大数据搜索引擎调词平台价格
  • 有哪些网站是用php做的莱芜网页定制
  • app开发和网站开发哪个简单wordpress H2标签
  • 专注湛江地区网站开发定制建设南京seo网络推广
  • com是什么网站色无极网站正在建设中
  • 上海工程建设执业资格注册中心网站wordpress 升级 权限
  • 安徽工程建设信息网站怎么制作一个网站及小程序
  • 模板网站建设合同wordpress主机转移
  • 做网站纸张大小青岛圭谷网站建设公司
  • 潍坊公司网站制作系统管理网站
  • 凡科做的网站为什么搜不到做竞彩网站代理犯法么
  • 西安网站开发公司哪家好宁波网站公司哪里好