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. Python-小游戏题目

    猜年龄游戏 n = 0 rayn_age = 19 a = {0:'666',1:'777',2:'888'} while n <3: age = input('请输入你的年龄:') age = ...

  2. (转))iOS App上架AppStore 会遇到的坑

    iOS App上架AppStore 会遇到的坑   前言:非原创 文章摘自:http://zhuanlan.zhihu.com/100000PM/20010725 相信大家一定非常「深恶痛疾」AppS ...

  3. (原)剑指offer跳台阶和矩形覆盖

    跳台阶 时间限制:1秒空间限制:32768K 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   分析同样为斐波那契数列边形这样的题肯定有公式 设 ...

  4. log4net.dll配置以及在项目中应用

    1,首先在项目中引用log4net.dll,然后项目中添加一个配置文件log4net.config <?xml version="1.0" encoding="ut ...

  5. BNUOJ 1207 滑雪

    滑雪 Time Limit: 1000ms Memory Limit: 65536KB   This problem will be judged on PKU. Original ID: 10886 ...

  6. zoj 2679 Old Bill

    Old Bill Time Limit: 2 Seconds      Memory Limit: 65536 KB Among grandfather��s papers a bill was fo ...

  7. 爆炸几何之 CCPC网络赛 I - The Designer (笛卡尔定理)

    本文版权归BobHuang和博客园共有,不得转载.如想转载,请联系作者,并注明出处.   Nowadays, little hahahaha got a problem from his teache ...

  8. JavaScript onload

     The onload event occurs immediately after a page or an image is loaded.onload事件当一个页面或是一张图片加载完成时被触发. ...

  9. 在windows下安装flex和bison、GCC

    学习Stellar-core 需要依赖项flex .bison .gcc三个依赖项 下载得网址:链接: https://pan.baidu.com/s/1mitCLcs 密码: 3jaj   通过 w ...

  10. POJ-1696 Space Ant 凸包版花式水过!

                                                         Space Ant 明天早上最后一科毛概了,竟然毫无复习之意,沉迷刷题无法自拔~~ 题意:说实 ...