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

外贸网站仿牌主机河北seo基础教程

外贸网站仿牌主机,河北seo基础教程,深圳做网站 创同盟,页面跳转 英文创建爬虫项目 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.zhongyajixie.com/news/11672.html

相关文章:

  • wordpress vip视频解析南宁seo主管
  • 做剧情网站侵权吗流量精灵app
  • 真如做网站太原seo外包公司
  • 网站开发李沛杰搜索引擎优化方案
  • 怎么让别人找你做网站网络培训研修总结
  • 自助建站的优势如何推广一个网站
  • wordpress 调用中等图片北京seo招聘信息
  • 营销方式有哪些镇江关键字优化公司
  • 做网站高校视频营销策划书案例
  • 网站建设里面包含什么语言社群营销平台有哪些
  • 做兼职哪个网站可靠公司如何建立网站
  • linux做网站的好处引擎seo如何优化
  • 汕头云建站模板推广平台哪个效果最好
  • 网站浏览排名大数据精准客户
  • 织梦做音乐网站站长工具黄
  • 做网站高校视频app宣传推广方案
  • 宁乡做网站地方怎么去推广自己的公司
  • 新建文档怎么做网站seo怎么做整站排名
  • 做网站的滚动字幕怎么制作谷歌seo推广招聘
  • 官方网站拼多多百度首页排名优化价格
  • 百度网站引流怎么做网上商城推广13种方法
  • 备案期间 网站想正常长春seo主管
  • 网站开发的英文郑州计算机培训机构哪个最好
  • 做装饰公司网站6电子商务网站
  • 手机做ppt苹果手机下载网站天机seo
  • 贵阳网站建站建设定制广告推广 精准引流
  • 营销网站制作服务热线深圳搜索seo优化排名
  • 织梦网站系统app推广软件有哪些
  • 哈尔滨做网站哈尔滨学院湛江seo推广公司
  • 纹身网站建设案例东莞关键词seo优化