推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shawndev
V2EX  ›  Python

关于 scrapy 使用 twisted.enterprise.adbapi 执行 SQL 的问题。

  •  
  •   shawndev · Sep 28, 2017 · 3388 views
    This topic created in 3160 days ago, the information mentioned may be changed or developed.
    问题是这样的,在 scrapy 中使用 pipeline 保存数据到 MySQL。
    执行 SQL 语句例如
    execute(
    """
    INSERT INTO table_name(field1, field2, field3, field4)
    VALUES(%s, %s, %s, %s)

    """, % (item.name, item.age, item.sex, item.loc))
    会提示 SYNTAX NEAR 'INSERT INTO table_name(filed1, fi\' 错误,这里的 SQL 会被提示折断。当修改 SQL 减少插入的字段数量以缩短单行文本长度后,BUG 又会莫名的小时。

    每次运行 scrapy crawl 都会提示这样的错误信息,但是经过几次运行数据库居然也有了十几条数据。
    请问有人遇到类似的问题吗,python 新手,如果问题实在太低端请轻喷。
    2 replies    2017-09-28 09:11:19 +08:00
    shawndev
        1
    shawndev  
    OP
       Sep 28, 2017
    数据可以正常爬取并导出到 JSON 文件。打印过了抓取的网页也没有问题,目前怀疑是多行文本即 string literal 出了问题,对 python 不够熟悉还请高人指点。
    flamepeak
        2
    flamepeak  
       Sep 28, 2017
    不知道具体原因,我的代码给你看一下:

    sql_insert = "insert into articles (category,tags,url,title,author) values (%s,%s,%s,%s,%s)"
    sql_insert_params = (category, item['tags'],item['link'],item['title'],item['author'])
    tx.execute(sql_insert, sql_insert_params)

    多试几次吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 08:57 · PVG 16:57 · LAX 01:57 · JFK 04:57
    ♥ Do have faith in what you're doing.