【问题标题】:Passing scrapy resutls to mysql databasee将scrapy结果传递给mysql数据库
【发布时间】:2018-12-06 10:48:15
【问题描述】:

我正在尝试构建一个小型刮板来将一些新闻主题分类为一个爱好项目(我不是专业的开发人员或技术人员,我是 OOP 和 Python 的初学者,我有一些 php 和arduino 编程语言)。我设法理解了scrapy和partialy mysql管道。如果我用一个简单的字符串替换 item['titlu'] 和 item['articol'],数据库将被填充。我搜索并阅读了很多信息,但我完全无法解决我的问题。 我想 item['titlu'] 和 item['articol'] 是某种类型的数组或mysql不喜欢的东西。我将发布代码和错误以寻求帮助。 注释的代码行是我解决问题的一些尝试 mysql数据库表是:

CREATE TABLE `ziare_com` (
  `id` int(11) NOT NULL,
  `titlu` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `articol` varchar(20000) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

我还尝试将titlu 和articol 文本类型更改为varchar。我故意让这样的表格示例(带有一个字段文本和其他 varchar)让您知道我尝试了哪些设置。

谢谢:

蜘蛛:

  def parse(self, response):
     #pass
     for link in response.xpath('//h2[@class="titlu_sec"]/a/@href').extract():
         yield response.follow(link, callback=self.parse_detail)
 def parse_detail(self, response):
     item = RezultScrap()
     #for quote in response.css('div.quote')
     item['titlu'] = response.css(".titlu_stire::text").extract()
     item['articol'] = response.css(".descriere_main::text").extract()
     return item

         #item['titlu'] = response.xpath('//div[contains(@id, "interior_left")]/h1/text()').extract_first()
         #item['articol'] = response.xpath('//div[contains(@id, "content_font_resizable")]//text()').extract()
     #titlu = response.css(".titlu_stire::text").extract()
     #articol = response.css(".descriere_main::text").extract()
         #yield item
     #titul1 = re.sub(r"['\\]","", titlu)
     #articol1 =  re.sub(r"['\\]","", articol)


    # yield {
     #        'titlu':titlu,
      #       'articol':articol
             #titlu,
             #articol
     #}

items.py:

import scrapy


 class FirstItem(scrapy.Item):
     # define the fields for your item here like:
     # name = scrapy.Field()
     pass
 class RezultScrap(scrapy.Item):
     titlu=scrapy.Field()
     articol=scrapy.Field()

pipelines.py:

import pymysql
 #from scrapy.exceptions import DropItem
 #pmysql.escape_string("'")
 from first.items import RezultScrap


 class Mysql(object):
         def __init__(self):
             self.connection = pymysql.connect("localhost","xxxxxx","xxxx","ziare")
             self.cursor = self.connection.cursor()


         def process_item(self, item, spider):
             #titlu1 = [pymysql.escape_string(item['titlu'])]
             #articol1 = [pymysql.escape_string(item['articol'])]
             #self.cursor.execute
             #query ="INSERT INTO ziare_com (titlu, articol) VALUES (%s, %s)"
             query ="INSERT INTO ziare_com (titlu, articol) VALUES (%s, %s) % (item['titlu'], item['articol'])"
             self.cursor.execute(query)
             #self.cursor.executemany(query)
             self.connection.commit()
             #return item

         def close_spider(self, spider):
             self.cursor.close()
             self.connection.close()

错误如下:

这是我使用 self.cursor.executemany(query) 的时候

TypeError: executemany() 缺少 1 个必需的位置参数: '参数'

这是我使用 self.cursor.execute(query) 的时候 我明白了:

pymysql.err.ProgrammingError: (1064, "你的 SQL 有错误 句法;检查与您的 MySQL 服务器版本相对应的手册 在 '%s, %s) % (item['titlu'], item['articol'])' 在第 1 行")

【问题讨论】:

  • 尝试在变量周围设置引号并从字符串中移出元组:'INSERT INTO ziare_com (titlu, articol) VALUES ("%s", "%s")' % (item['titlu '], 项目['articol'])

标签: python mysql arrays variables scrapy


【解决方案1】:

process_item 中的正确代码是

def process_item(self, item, spider):

    query ="INSERT INTO ziare_com (titlu, articol) VALUES (%s, %s)"
    self.cursor.execute(query, [ item['titlu'], item['articol'] ])
    self.connection.commit()
    return item

您只需要为值编写%s,然后在execute 方法中将它们作为列表(数组)传递

此外,你应该了解字符串格式化

你在做

"INSERT INTO ziare_com (titlu, articol) VALUES (%s, %s) % (item['titlu'], item['articol'])"

整个是一个字符串,你根本没有将值传递给你的字符串

这里是更正的陈述

"INSERT INTO ziare_com (titlu, articol) VALUES (%s, %s) " % ((item['titlu'], item['articol']))

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2015-08-24
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多