RenBingo 2019-05-23 17:37 采纳率: 0%
浏览 864

scrapy爬虫, pipelines 中无法打印出东西

爬取文件

import scrapy

from  toutiao.items import ToutiaoItem

class TestSpider(scrapy.Spider):
    name = 'test'
    allowed_domains=['weixin.sogou.com','mp.weixin.qq.com']
    start_urls = [
        "https://weixin.sogou.com/",
        "https://weixin.sogou.com/pcindex/pc/pc_0/1.html"
    ]
    def parse(self, response):
        node_list=response.xpath('//ul/li/div[@class="img-box"]/a/@href').extract()[:1]
        for node in node_list:
            item = ToutiaoItem()
            item['url']=response.url
            item['title']='test title'
            item['content']='test1 content'
            yield item

pipelines 文件

# -*- 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  pymysql

class ToutiaoPipeline(object):
    def __init__(self):
        self.connect=pymysql.connect(host='127.0.0.1',user='test',password='123456',db='bin_xinwen',port=3306)
        self.cursor=self.connect.cursor()
        def process_item(self, item, spider):
            print(11111111111111111111111)
            print(item['url'])
            print(22222222222222222222222)
            # self.cursor.execute('insert into articles(url,title,content) VALUES ("{}","{}","{}")').format(item['url'],item['title'],item['content'])
            # self.cursor.commit()
            return item
        def  close_spider(self,spider):
            self.cursor.close()
            self.connect.close()


settings.py

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

BOT_NAME = 'toutiao'

SPIDER_MODULES = ['toutiao.spiders']
NEWSPIDER_MODULE = 'toutiao.spiders'


ROBOTSTXT_OBEY = False

SPIDER_MIDDLEWARES = {
   'toutiao.middlewares.ToutiaoSpiderMiddleware': 543,
}

# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'toutiao.pipelines.ToutiaoPipeline': 100,
}


执行的时候打印

2019-05-23 17:43:07 [scrapy.middleware] INFO: Enabled item pipelines:
['toutiao.pipelines.ToutiaoPipeline']

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 18:31
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型