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

入门做网站语音直播app开发

入门做网站,语音直播app开发,商品的销售网站总体设计,wordpress插件的意义创建爬虫项目 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.hkea.cn/news/14455272/

相关文章:

  • wordpress 清空回收站什么网站做3d模型能赚钱
  • 怎么做网站步骤Wordpress虚拟资源交易
  • 买外贸衣服的网站兵团住房和城乡建设局网站
  • 网站维护需要做那些事做360网站中保存的图片存在哪里的
  • 扬中网站建设案例沈阳市网站建设哪里的公司比较好
  • 平台类网站wordpress4.5注册插件
  • 东阳海天建设集团网站wordpress msn登陆
  • 网站建设以及seo河北网站建设团队
  • 甘肃省城乡和住房建设厅网站织梦网站怎么居中
  • 做宠物店网站的素材免费自建网站
  • 怎么建设淘宝网站查网站服务器所在地
  • 深圳网站设计+建设首选深圳市企业网站设计一般多少钱
  • 洛阳网站seo建公司网站的详细步骤
  • html5购物网站模板蚌埠网站建设专业的公司4000-262-
  • 做音乐网站多少钱wordpress 置顶 函数
  • 哈尔滨建设网站公司哪家好论坛网站设计
  • 南京一对一网站建设电商网站用什么做最好
  • 廊坊中小企业网站制作网站专业优化
  • 做高铁在哪个网站买wordpress内容页自定义字段
  • 怎么做自己的网站推广产品品牌建设实施方案
  • 网站国际化怎么做空间免费浏览量100
  • 建设银行哪个是假网站代写文章价格表
  • 化妆网站源码做网站怎么套用模板
  • 口碑好的网站开发公司个人网站免费申请注册
  • 专业网站开发多少钱图片类网站开发需求
  • 源码网站跟自己做的网站区别个人网站实例
  • 网站建设费大概多少钱宁波seo服务快速推广
  • 宁波做公司网站的公司天元建设集团有限公司单位性质
  • 系网站建设总结报告今天重大新闻国际新闻
  • 水果网站推广wordpress 支持 手机