当前位置: 首页 > 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.dnsts.com.cn/news/22267.html

相关文章:

  • 说服企业做网站网站幻灯片效果
  • 烟台h5网站开发怎样做境外网站
  • 手机网站meta心连网网站
  • 珠海市网站建设哪家好青岛 php 网站建设
  • 重庆规模最大的建网站公司东莞营销网站建设
  • 西安网站seo诊断wordpress版权破解
  • 加盟企业网站建设目的海阳手机网站开发
  • wap网站生成小程序织梦网站如何做关键词
  • 资源丰富免费的网站推荐排名企业型网站开发
  • 做钢管用哪个门户网站网页制作的工具有哪些
  • 网站建设费用的会计如何快速推广一个新产品
  • 网站怎么推广效果好昆明市住房和城乡建设局官方网站
  • 湖南seo网站设计网站建站网站多少钱
  • 视频网站设计与开发seo百度排名优化
  • 成都网站建设开发公司内江市建设培训中心网站
  • 移动端官网佛山旺道seo优化
  • jsp怎么做视频网站搜索引擎营销成功的案例
  • 求推荐个网站wordpress 字体不好看
  • wordpress中运行程序吐鲁番seo招聘
  • iis搭建网站时 属于默认文档的是六安网站
  • 合肥网站建设索q479185700番禺大石做网站
  • 阿里巴巴网站建设的背景海南注册家族公司条件
  • 福州php做网站php网站开发实例教程思考题
  • 资阳市住房和城乡建设局网站科技感网站
  • 网站备案不通过phpcms v9网站导航
  • 本地搭建网站网站后台保定网站建设公司排名
  • 企业网站搭建流程有名的软件开发公司
  • 信得过的网站开发推广电商网站话费充值怎么做
  • 西安网站建设推广服务长沙大型公司
  • 德阳公司网站建设phalapi wordpress