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

咸阳做网站优化网站注册查询官网

咸阳做网站优化,网站注册查询官网,枣强网站建设,深圳网站建设公司简介创建爬虫项目 srcapy startproject scrapy_dangdang进入到spider文件里创建爬虫文件(这里爬取的是青春文学,仙侠玄幻分类) srcapy genspider dang http://category.dangdang.com/cp01.01.07.00.00.00.html获取图片、名字和价格 # 所有的se…
  1. 创建爬虫项目
srcapy startproject scrapy_dangdang
  1. 进入到spider文件里创建爬虫文件(这里爬取的是青春文学,仙侠玄幻分类)
srcapy genspider dang http://category.dangdang.com/cp01.01.07.00.00.00.html
  1. 获取图片、名字和价格
# 所有的seletor的对象,都可以再次调用xpath方法
li_list = response.xpath('//div[@id="search_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[@class="price"]/span[1]/text()').extract_first()print(src, name, price)
  1. 在items里定义要下载的数据
import scrapyclass ScrapyDangdang39Item(scrapy.Item):# 要下载的数据都有什么# 图片src = scrapy.Field()# 名字name = scrapy.Field()# 价格price = scrapy.Field()
  1. 在dang.py里导入items
from ..items import ScrapyDangdang39Item
  1. 在parse方法里定义一个对象book,然后把获取到的值传递到pipelines
book = ScrapyDangdang39Item(src=src, name=name, price=price)# 获取一个book就将book传递给pipelines
yield book
  1. 开启管道
    在settings中,把这几行代码取消注释
    在这里插入图片描述
    管道可以有很多个,但是管道是有优先级的,优先级的范围是1到1000 值越小,优先级越高
  2. 下载数据
    打开piplines.py
class ScrapyDangdang39Pipeline:# 方法1# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp = open('book.json', 'w', encoding='utf-8')def process_item(self, item, spider):# item就是yield后面的book对象# 1.write方法必须要写一个字符串,而不是其他的对象# 2.w模式,每一个对象都会打开一次文件,然后覆盖之前的内容,所以使用a模式with open('book.json', 'a', encoding='utf-8')as fp:fp.write(str(item))return item

但是这种模式不推荐,因为每传递过来一个数据,就要打开一次文件,对文件的操作太过频繁
换一种方法

class ScrapyDangdang39Pipeline:# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp = open('book.json', 'w', encoding='utf-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()
  1. 运行dang.py文件就可以把数据保存到本地了

完整代码
dang.py

import scrapy
from ..items import ScrapyDangdang39Itemclass DangSpider(scrapy.Spider):name = "dang"allowed_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[@id="search_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[@class="price"]/span[1]/text()').extract_first()book = ScrapyDangdang39Item(src=src, name=name, price=price)# 获取一个book就将book传递给pipelinesyield book

items.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
#
# Don't 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', encoding='utf-8')def process_item(self, item, spider):# item就是yield后面的book对象# 这种模式不推荐# with open('book.json', 'a', encoding='utf-8')as fp:#     fp.write(str(item))self.fp.write(str(item))return item# 在爬虫文件执行完后执行的一个方法def close_spider(self, spider):self.fp.close()
http://www.wangmingla.cn/news/8278.html

相关文章:

  • app网站模板下载不了如何制作网站赚钱
  • 苏州外贸网站制作今日新闻播报
  • 点网站出图片怎么做互联网域名注册查询
  • 广告策划书籍百度seo快速见效方法
  • 黑龙江做网站哪家好智能建站系统
  • 北京华诚传媒有限公司官方网站市场推广策略
  • 苏州工业园区公共资源交易中心推推蛙贴吧优化
  • 网站优化怎么做ppt新闻式软文范例
  • 国外怎么做直播网站自己怎么做网页推广
  • 盐城网站建设制作方案百度seo提高排名费用
  • 免费手机端网站模板下载安装外贸建站
  • 常州网站开发培训价格产品软文范例大全
  • 网站建设网店名字谷歌浏览器下载手机版安卓官网
  • 建网站用html百度怎么进入官方网站
  • 如何搜索易思cms做的网站如何优化标题关键词
  • 青州哪里做网站大数据统计网站
  • 福州学做网站百度百科优化
  • 做网站要多少回扣成功营销十大经典案例
  • 互联网网站 有哪些网络外包
  • 多米诺网站建设高端企业网站模板
  • 淘宝客网站一定要备案吗百度网页链接
  • 套模板做网站教程东莞企业网站排名优化
  • 前端做的好的网站武汉seo推广
  • 国外购物网站有哪些友情链接如何交换
  • 东营网站建设seo福州网站排名推广
  • 公司长沙建站中国十大公关公司排名
  • 营销手机网站版面怎么seo快速排名
  • 微站网建站系统广州网站营销seo
  • 想学做网站学什么教程百度推广优化工具
  • 国内网站开发平台哪家强南宁seo排名收费