向mysql数据库传递scrapy resutls

2024-04-26 23:44:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个小刮板排序一些新闻主题作为一个爱好项目(我不是一个专业的开发人员或技术人员,我是一个面向对象和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() missing 1 required positional argument: 'args'

这就是我使用self.cursor.execute执行(查询) 我明白了:

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s) % (item['titlu'], item['articol'])' at line 1")


Tags: textselfforresponsedefextractutf8item
1条回答
网友
1楼 · 发布于 2024-04-26 23:44:25

过程中的正确代码是

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

相关问题 更多 >