将scrapy resutls传递到mysql数据库

krugob8w  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(354)

我试图建立一个小刮板排序一些新闻主题作为一个爱好项目(我不是一个专业的开发人员或技术人员,我是一个面向对象和python的初学者,我有一些php和arduino编程语言的经验)。我总算明白了我的意思。如果我用一个简单的字符串替换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。为了让您知道我尝试了哪些设置,我特意让这个表示例(带有一个filde text和另一个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
     #}

项目.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()

管道.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()

EROR如下:
这是当我使用self.cursor.executemany(查询)时
typeerror:executemany()缺少1个必需的位置参数:“args”
当我使用self.cursor.execute(query)时得到:
pymysql.err.programmingerror:(1064,“您的sql语法有错误;请检查与您的mysql server版本对应的手册,以了解在“%s,%s)%附近使用的正确语法(第1行的item['titlu',item['articol'))

vd8tlhqk

vd8tlhqk1#

过程中的正确代码是

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']))

相关问题