settings.py设置

ITEM_PIPELINES = {
'tets.pipelines.TetsPipeline': 300,
}

spider代码

xpath后缀添加.extract() parse()返回return item

import scrapy
from tets.items import TetsItem class KugouSpider(scrapy.Spider):
name = 'kugou'
allowed_domains = ['www.kugou.com']
start_urls = ['http://www.kugou.com/'] def parse(self, response):
item = TetsItem()
item['title'] = response.xpath("/html/head/title/text()").extract()
print(item['title'])
return item

piplines代码

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import json class TetsPipeline(object):
def __init__(self):
# self.file = codecs.open("D:/git/learn_scray/day11/mydata2.txt", "wb", encoding="utf-8")
self.file = codecs.open("D:/git/learn_scray/day11/1.json", "wb", encoding="utf-8") # 处理文本(xx.txt)
# def process_item(self, item, spider):
# l = str(item) + "\n"
# print(l)
# self.file.write(l)
# return item
def process_item(self, item, spider):
print("进入")
# print(item)
i = json.dumps(dict(item), ensure_ascii=False)
# print("进入json")
# print(i)
l = i + "\n"
print(l)
self.file.write(l)
return item def close_spider(self, spider):
slef.file.close()

items.py

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class TetsItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field()

结果如图下

深入爬虫书scrapy 之json内容没有写入文本的更多相关文章

  1. 服务端JSON内容中有富文本时

    问题背景 由于数据中存在复杂的富文本,包含各种引号和特殊字符,导致后端和前端通过JSON格式进行数据交互引发前端JSON解析出错. 解决方案 后端将富文本内容 ConvertToBase64Strin ...

  2. python根据索引删除内容并写入文本

    在python中,有个好用的模块linecache,该模块允许从任何文件里得到任何的行,并且使用缓存进行优化,常见的情况是从单个文件读取多行.linecache.getline(filename,li ...

  3. scrapy(四): 爬取二级页面的内容

    scrapy爬取二级页面的内容 1.定义数据结构item.py文件 # -*- coding: utf-8 -*- ''' field: item.py ''' # Define here the m ...

  4. Scrapy 框架 使用 selenium 爬取动态加载内容

    使用 selenium 爬取动态加载内容 开启中间件 DOWNLOADER_MIDDLEWARES = { 'wangyiPro.middlewares.WangyiproDownloaderMidd ...

  5. Learning Scrapy笔记(六)- Scrapy处理JSON API和AJAX页面

    摘要:介绍了使用Scrapy处理JSON API和AJAX页面的方法 有时候,你会发现你要爬取的页面并不存在HTML源码,譬如,在浏览器打开http://localhost:9312/static/, ...

  6. 爬虫框架scrapy的基本内容

    Scrapy介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以帮助用户简单快速的部署一个专业的网络爬虫.如果说前面我们写的定制bs4爬虫是”手动挡“,那Scrapy就相当 ...

  7. 【C#】菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

    VS 2015菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

  8. .net mvc web api 返回 json 内容,过滤值为null的属性

    原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...

  9. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...

随机推荐

  1. Bootstrap历练实例:警告样式按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  2. OpenCV Haar AdaBoost源码改进据说是比EMCV快6倍

    <pre name="code" class="cpp">#include "Haar.h" #include "lo ...

  3. javaEE(17)_邮件原理与JavaMail开发

    一.Java邮件开发介绍 为什么要学习javamail开发 •现在很多WEB应用在开发时都需要集成邮件发送功能,例如: •给新注册的用户自动发送一封包含其注册信息的欢迎E-Mail. •给过生日的注册 ...

  4. ios之UIScrollView

    UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 [java] view plaincopy     CGRect bounds = [ [ UIScreen mainSc ...

  5. GIMP的Path的import和export

    点击Path栏中的小三角,选择Paths Menu,然后点击Export Path Import Path自然不必多说:

  6. logstash-基础操作

    一.环境 https://www.cnblogs.com/suffergtf/p/9566145.html 1.收取日志文件到elasticsearch [root@linux-node1 logst ...

  7. 如何用纯 CSS 创作一个菱形 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKzjqK 可交互视频教 ...

  8. 小数据池 is 和 ==的区别

    小数据池 一.小数据池 1)代码块 python程序是由代码块构成的,一个代码块的文本作为pythont程序执行的单元 官方文档: A Python program is constructed fr ...

  9. CSS里总算是有了一种简单的垂直居中布局的方法了

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. Matplotlib基本图形之条形图2

    Matplotlib基本图形之条形图2 1.绘制多条条形图 示例代码: import time import numpy as np import matplotlib.pyplot as plt b ...