2

I'm trying to save the data scraped with scrapy to mysql. But, I have these issues:

  1. No more support for MySQLdb. So, I have to use

    import pymysql

    pymysql.install_as_MySQLdb() on settings.py file

  2. On python 3 %s is deprecated and I have to use .format with the following code:

def close(self, reason):
        csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)       
        mydb = MySQLdb.connect(host='localhost',
                               user='demo',
                               passwd='123456',
                               db='testdb')
        cursor = mydb.cursor()

        csv_data = csv.reader(open(csv_file))

        row_count = 0
        for row in csv_data:
            if row_count != 0:
                cursor.execute("INSERT IGNORE INTO testtb(product, category) VALUES('{}','{}')".format(*row))
            row_count += 1

        mydb.commit()
        cursor.close()

I have the following error

<bound method AutorSpider.close of <AutorSpider 'autor' at 0x7f64725d29b0>>

Traceback (most recent call last):
  File "/home/pc/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
    result = f(*args, **kw)
  File "/home/pc/.local/lib/python3.6/site-packages/pydispatch/robustapply.py", line 55, in robustApply
    return receiver(*arguments, **named)
  File "/home/pc/Escritorio/fpyautor/fpyautor/spiders/autor.py", line 109, in close
    cursor.execute("INSERT IGNORE INTO autortb(frase, categoria) VALUES({},'{}')'".format(*row))
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
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 'titulo del item numero 1' at line 1")

is other simplier/eficcient way? because im saving the data at the end of the scraping task and if i got more results (3000 items )maybe this is a problem in future with bigger sites?

23
  • is product a varchar column ? Commented Aug 2, 2018 at 19:04
  • yes, thanks for comment Commented Aug 2, 2018 at 19:05
  • And your braces are not properly closed. Commented Aug 2, 2018 at 19:07
  • edited, edited. Commented Aug 2, 2018 at 19:08
  • Wait, are you still getting the error? Commented Aug 2, 2018 at 19:15

1 Answer 1

1

escape string can help you

def close(self, reason):
    csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)       
    mydb = MySQLdb.connect(host='localhost',
                           user='demo',
                           passwd='123456',
                           db='testdb')
    cursor = mydb.cursor()

    csv_data = csv.reader(open(csv_file))

    row_count = 0
    for row in csv_data:
        if row_count != 0:
            product = mydb.escape_string(row[0])
            category = mydb.escape_string(row[1])
            #print category , product
            sql = 'INSERT IGNORE INTO testtb(product, category) VALUES ( "{}","{}")'.format(product,category)
            #print sql
            cursor.execute(sql)
            row_count += 1

    mydb.commit()
    cursor.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.