请教个爬 虫存 mysql 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Colorful
V2EX    Python

请教个爬 虫存 mysql 的问题

  •  
  •   Colorful 2020-05-07 21:46:21 +08:00 2816 次点击
    这是一个创建于 1988 天前的主题,其中的信息可能已经有所发展或是发生改变。

    import pymongo

    from pymongo import MongoClient

    import pymysql as pq

    class Pipeline(object): def init(self): self.cOnn= pq.connect(host='127.0.0.1', user='root',port='3306', passwd='123456', db='root', charset='utf8') self.cur = self.conn.cursor() # def init(self): # host = '127.0.0.1' # port = 27017 # client = pymongo.MongoClient(host=host,port=port) # collection = client.local.letsgojpt # self.post = collection

    def process_item(self, item, spider): for i in range(0,len(item["title_all"])): # sql = "insert into letsgojp(title_all, center_all) VALUES (%s, %s)" self.cur.execute("insert into letsgojp(title_all,center_all) values(%s,%s)",(item['title_all'][i],item['center_all'][i])) self.conn.commit() # self.post.insert(data) return item def close_spider(self, spider): self.cur.close() self.conn.close() 

    主要是这行报错

    self.cur.execute("insert into letsgojp(title_all,center_all) values(%s,%s)",(item['title_all'][i],item['center_all'][i]))

    用 mongo 的时候是正常存数据的,但是用 mysql 就报错,网上找一些,不太会用,请教各位大神了

    14 条回复    2020-05-08 16:19:26 +08:00
    ClericPy
        1
    ClericPy  
       2020-05-07 21:56:44 +08:00
    1. 找个贴代码的地方把缩进留下...

    2. 报错信息解释 90% 问题, 大部分时间报错信息最后一行一搜 stackoverflow 上都有同行遇到过

    3. 纯看的话, 实在猜不到啊... 就语法来说, pymysql 会自动转义, 问题不大, 看你的列名也没有敏感关键字所以不加反引号也没事
    jugelizi
        2
    jugelizi  
       2020-05-07 22:00:52 +08:00
    [item['title_all'][i],item['center_all'][i]] 是不是?
    也有可能没处理好特殊字符 错误提示贴下啊
    Colorful
        3
    Colorful  
    OP
       2020-05-07 22:17:51 +08:00
    @jugelizi 应该不会的吧,因为存 mongo 的时候是没总是的
    chaneyccy
        4
    chaneyccy  
       2020-05-07 22:55:09 +08:00
    不贴报错信息,这也看不出来哪里有问题吧

    没用过 SQL 好奇搜了一圈,没有发现模块导入是 import pymysql as pq,问题在这?
    Colorful
        5
    Colorful  
    OP
       2020-05-07 23:05:28 +08:00
    @chaneyccy 不是那里,能爬到一条数据
    self.cur.execute("insert into letsgojp(title_all,center_all) values(%s,%s)",(item['title_all'][i],item['center_all'][i]))

    主要就是这行
    aydd2004
        6
    aydd2004  
       2020-05-07 23:17:58 +08:00
    execute 的内容 print 出来看一下

    实在不行粘到 mysql 里面执行一下
    dorothyREN
        7
    dorothyREN  
       2020-05-08 00:01:50 +08:00
    占位符那里的问题,插入字符串 需要 加上 \'%s\'
    dorothyREN
        8
    dorothyREN  
       2020-05-08 00:06:12 +08:00
    @dorothyREN #7 ``` def save_data(self, title, content):
    sql = "INSERT INTO test(title,content) VALUES (\'%s\',\'%s\')" % (title, content)
    # print(sql)
    self.cur.execute(sql)
    self.conn.commit()
    ```
    crella
        9
    crella  
       2020-05-08 07:24:08 +08:00 via Android
    mysql 新版的 mb-utf8 编码设置有点麻烦。

    建议直接 postgresql
    triangle111
        10
    triangle111  
       2020-05-08 08:51:33 +08:00
    print 一下 sql 语句到 mysql 执行就知道错哪了, 应该是字符串没有引号导致的吧
    annielong
        11
    annielong  
       2020-05-08 09:20:56 +08:00
    一般都是编码和非法字符的问题,打印 sql 语句就可以看出来
    zhangysh1995
        12
    zhangysh1995  
       2020-05-08 13:58:46 +08:00
    添加 try except,抓 mysql.connector.errors.DatabaseError 错误
    leapV3
        13
    leapV3  
       2020-05-08 15:04:30 +08:00
    你试试将表名与列名加上反引号
    AmberJiang
        14
    AmberJiang  
       2020-05-08 16:19:26 +08:00
    感觉像是 insert 语句那里存在语法错误。。。贴报错图出来看看吧
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     882 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 21:22 PVG 05:22 LAX 14:22 JFK 17:22
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86