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

电子商务网站规划书范文儿童手工

电子商务网站规划书范文,儿童手工,上海建设工程标准与造价信息网站,电商网站特点创建爬虫项目 srcapy startproject scrapy_dangdang进入到spider文件里创建爬虫文件#xff08;这里爬取的是青春文学#xff0c;仙侠玄幻分类#xff09; srcapy genspider dang http://category.dangdang.com/cp01.01.07.00.00.00.html获取图片、名字和价格 # 所有的se…创建爬虫项目 srcapy startproject scrapy_dangdang进入到spider文件里创建爬虫文件这里爬取的是青春文学仙侠玄幻分类 srcapy genspider dang http://category.dangdang.com/cp01.01.07.00.00.00.html获取图片、名字和价格 # 所有的seletor的对象都可以再次调用xpath方法 li_list response.xpath(//div[idsearch_nature_rg]//li)for li in li_list:# 获取图片src li.xpath(.//img/data-original).extract_first()# 第一张图片和其他图片的标签的属性不一样# 第一张图片的src是可以使用的其他图片的地址在data-original里if src:src srcelse:src li.xpath(.//img/src).extract_first()# 获取名字name li.xpath(.//img/alt).extract_first()# 获取价格price li.xpath(.//p[classprice]/span[1]/text()).extract_first()print(src, name, price)在items里定义要下载的数据 import scrapyclass ScrapyDangdang39Item(scrapy.Item):# 要下载的数据都有什么# 图片src scrapy.Field()# 名字name scrapy.Field()# 价格price scrapy.Field()在dang.py里导入items from ..items import ScrapyDangdang39Item在parse方法里定义一个对象book然后把获取到的值传递到pipelines book ScrapyDangdang39Item(srcsrc, namename, priceprice)# 获取一个book就将book传递给pipelines yield book开启管道 在settings中把这几行代码取消注释 管道可以有很多个但是管道是有优先级的优先级的范围是1到1000 值越小优先级越高下载数据 打开piplines.py class ScrapyDangdang39Pipeline:# 方法1# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp open(book.json, w, encodingutf-8)def process_item(self, item, spider):# item就是yield后面的book对象# 1.write方法必须要写一个字符串而不是其他的对象# 2.w模式每一个对象都会打开一次文件然后覆盖之前的内容所以使用a模式with open(book.json, a, encodingutf-8)as fp:fp.write(str(item))return item但是这种模式不推荐因为每传递过来一个数据就要打开一次文件对文件的操作太过频繁 换一种方法 class ScrapyDangdang39Pipeline:# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp open(book.json, w, encodingutf-8)def process_item(self, item, spider):# item就是yield后面的book对象self.fp.write(str(item))return item# 在爬虫文件执行完后执行的一个方法def close_spider(self, spider):self.fp.close()运行dang.py文件就可以把数据保存到本地了 完整代码 dang.py import scrapy from ..items import ScrapyDangdang39Itemclass DangSpider(scrapy.Spider):name dangallowed_domains [category.dangdang.com]start_urls [http://category.dangdang.com/cp01.01.07.00.00.00.html]def parse(self, response):# 所有的seletor的对象都可以再次调用xpath方法li_list response.xpath(//div[idsearch_nature_rg]//li)for li in li_list:# 获取图片src li.xpath(.//img/data-original).extract_first()# 第一张图片和其他图片的标签的属性不一样# 第一张图片的src是可以使用的其他图片的地址在data-original里if src:src srcelse:src li.xpath(.//img/src).extract_first()# 获取名字name li.xpath(.//img/alt).extract_first()# 获取价格price li.xpath(.//p[classprice]/span[1]/text()).extract_first()book ScrapyDangdang39Item(srcsrc, namename, priceprice)# 获取一个book就将book传递给pipelinesyield bookitems.py # Define here the models for your scraped items # # See documentation in: # https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass ScrapyDangdang39Item(scrapy.Item):# define the fields for your item here like:# name scrapy.Field()# 要下载的数据都有什么# 图片src scrapy.Field()# 名字name scrapy.Field()# 价格price scrapy.Field()pipelines.py # Define your item pipelines here # # Dont forget to add your pipeline to the ITEM_PIPELINES setting # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html# useful for handling different item types with a single interface from itemadapter import ItemAdapter# 如果想使用管道就必须在settings中开启管道 class ScrapyDangdang39Pipeline:# 方法1# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp open(book.json, w, encodingutf-8)def process_item(self, item, spider):# item就是yield后面的book对象# 这种模式不推荐# with open(book.json, a, encodingutf-8)as fp:# fp.write(str(item))self.fp.write(str(item))return item# 在爬虫文件执行完后执行的一个方法def close_spider(self, spider):self.fp.close()
http://www.tj-hxxt.cn/news/233737.html

相关文章:

  • 网站主页面设计哪个好90后做网站
  • 自学做网站需要学会哪些学动漫制作需要什么基础
  • 网站地图在线制作工具网站链接分析工具
  • 网站添加百度地图标注广西建设网个人登录
  • 电子商务网站建设任务分解试剂网站建设
  • 网站源码下载了没有管理后台20个优秀的响应式设计html5网站模板
  • 做爰全程的网站微商软件平台
  • 快速做效果图的网站叫什么区别广元网络推广
  • 网站建设模式怎么写做物流的网站有哪些内容
  • 外贸汽车网站有哪些成都市住房与城乡建设厅网站
  • 国外创意型网站设计山东自助seo建站
  • diy在线定制网站系统网页设计好的网站
  • 成都怎么成立网站幻影图片一键制作网站
  • 学校html网站模板模板网站与定制网站的区别
  • 腾讯 云上做网站教程深圳做官网的公司
  • 网站建站WordPress离线博客
  • 药材公司网站建设模板工信部网站黑名单查询
  • 愚人网站建设潍坊市城乡建设局网站
  • 临沂网站设计价格中山企业网站制作
  • 高职图书馆网站建设大赛学编程的app
  • 株洲网站建设开发专业团队图片高清
  • 网站开发的进度安排盐津铺子网络营销推广方法
  • 山西省建设招聘信息网站陕西省咸阳市建设银行网站
  • 有没有做卡哇伊的企业网站北京pk10盘制作网站建设
  • 个人博客网站模板下载机器配件做外贸上什么网站
  • c 语言可以做网站吗百度网盘官网
  • 天津市北辰区建设与管理局网站系统优化升级
  • 网站底部链接代码泉州仿站定制模板建站
  • 中国寰球工程有限公司网站设计wordpress接入打赏
  • 网站建设实训意义ps做的网站