scrapy 采集入库,下面的是入的 sqlite3 的代码,改成入库 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
python30
V2EX    Python

scrapy 采集入库,下面的是入的 sqlite3 的代码,改成入库 mysql 的,下面代码需要改动吗?

  •  
  •   python30 2020-07-29 23:26:39 +08:00 2034 次点击
    这是一个创建于 1949 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面是网上的一个入库 sqlite3 的,我要原样改成入库 mysql 的。 功能就是插入文章,在插入文章前需要得到文章分类的 id,如果有就根据分类名查询到分类 id, 如果没有就根据得到的资料生成分类并取得 id. 下面 sqlite3 的是可以的

    问题是 我要改成 mysql 的就不行了。具体看后面代码。

    class BookspiderPipeline(object): def __init__(self): DBpath = os.getcwd() + '/db.sqlite3' self.con = sqlite3.connect(DBpath) self.cur = self.con.cursor() def process_item(self, item, spider): self.cur.execute("SELECT id FROM books_tag WHERE tagname = ?", (item['categoryName'],)) tagID = self.cur.fetchone() if not tagID: self.cur.execute("INSERT INTO books_tag (tagname) VALUES (?)", (item['categoryName'],)) self.con.commit() self.cur.execute("SELECT id FROM books_tag WHERE tagname = ?", (item['categoryName'],)) tagID = self.cur.fetchone() tagID = tagID[0] print(tagID) self.cur.execute("SELECT id FROM books_book WHERE title = ?", (item['bookName'],)) bookID = self.cur.fetchone() if not bookID: self.cur.execute(''' INSERT INTO books_book (title, cover, author, intro, tag_id) VALUES (?,?,?,?,?) ''', (item['bookName'], item['cover'], item['author'], item['intro'], tagID)) self.con.commit() self.cur.execute("SELECT id FROM books_book WHERE title = ?", (item['bookName'],)) bookID = self.cur.fetchone() bookID = bookID[0] print(bookID) self.cur.execute('''INSERT INTO books_chapter (number, title, content, book_id) VALUES (?,?,?,?)''', (int(item['number']), item['chapterName'], item['chapterContent'], bookID)) self.con.commit() return item 

    我的代码:

    class ScMySqlPipeline(object): def __init__(self): dbargs = dict( host = '127.0.0.1', user = 'root', passwd = 'mysqlpasswd', db = 'info2222', port = 3306, cp_recOnnect= True, cursorclass = pymysql.cursors.DictCursor, charset = 'utf8mb4', use_unicode = True, ) self.dbpool = adbapi.ConnectionPool('pymysql', **dbargs) def process_item(self, item, spider): #代码执行到这里就出问题了。应该下面代码不对。 #下面是 先判断有没有这个分类,根据分类名查询数据库,如果有就把这个分类的 id 取出来赋值给 category_id 以便于在后面插入文章时用到这个分类 id, 如果没有的话就插入这个分类然后再得到她的分类 id 值 conn.execute("""select id from aainfo_category where name = %s""", (item['fenlei'], )) category_id = conn.fetchone() if not category_id: conn.execute("INSERT INTO aainfo_category (`name`, `slug`, `chaodai`, `num`, `description`, `jianjie`, `weburl`) VALUES (%s, %s, %s, %s, %s, %s, %s)", (item['category_name'], item['category_slug'], item['category_chaodai'], 0, item['category_description'], item['category_jianjie'], item['category_weburl'])) #conn.commit() conn.execute("""select id from aainfo_category where name = %s""", (item['fenlei'], )) category_id = conn.fetchone() print (category_id) category_id = category_id[0] print (category_id) if isinstance(item, vodItem): query = self.dbpool.runInteraction(self.insert_into_info, item).addErrback(self.handle_error) return item 

    self.insert_into_info 这个函数是文章插入 mysql 的代码。需要用到分类的 id ,所以上面代码取得分类的 id 的功能。不知那里有错?请各位朋友指点一下。谢谢。

    3 条回复    2020-08-03 12:28:30 +08:00
    ooh
        1
    ooh  
       2020-07-29 23:58:20 +08:00
    你直接把报错贴出来呗
    conn.execute("""select id from aainfo_category where name = %s""", (item['fenlei'], ))
    这个 SQL 里面字符串占位符需不需要包起来
    conn.execute("""select id from aainfo_category where name = ‘%s‘""", (item['fenlei'], ))
    ooh
        2
    ooh  
       2020-07-30 00:02:28 +08:00
    @ooh 还有你 Cursor 对象去那里了

    cOnnection= pymysql.connect(host=MYSQL_HOST,
    user=MYSQL_USER,
    password=MYSQL_PASS,
    db=MYSQL_DB,
    charset='utf8mb4',
    cursorclass=pymysql.cursors.DictCursor)

    cursor = connection.cursor()

    // insert

    sql = 'INSERT INTO table (name) VALUES ("name")'
    cursor.execute(sql)
    connection.commit()
    // first
    sql = 'SELECT id,name FROM table'
    cursor.execute(sql)
    item = cursor.fetchone()
    python30
        3
    python30  
    OP
       2020-08-03 12:28:30 +08:00
    @ooh
    谢谢已经解决了

    执行 conn.fetchone() 后
    发现得到的数据是字典
    用 category_id = category_id['id'] 就可以得到查询的 id 了
    然后就可以用手后面的插入了

    ```
    conn.execute("""select id from scinfo_category where weburl = %s""", (category_url, ))
    category_id = conn.fetchone()
    category_id = category_id['id']

    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2860 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 00:17 PVG 08:17 LAX 16:17 JFK 19:17
    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