![](/img/trans.png)
[英]Error to import rss feeds tag pubDate into MySQL database using python
[英]import rss feeds into MySQL database using python
我正在嘗試使用python將提要導入到以pythoneverywhere托管的Mysql數據庫中,但是我似乎無法使其工作。 這是我正在使用的代碼。
import MySQLdb
import feedparser
myfeed = feedparser.parse("http://www.couponchief.com/rss/")
for item in myfeed['items']:
title = item.title
link = item.link
description = item.description
print title
print link
print description
db =
MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default');
with db:
cur = db.cursor()
cur.execute("INSERT INTO ini(title, link, description)VALUES(%s,
%s, %s)", (title,link,description))
print 'Succesfull!'
# close the cursor
cur.close()
# close the connection
db.close ()
有什么建議么?
可能您正在使用的MySQLdb較舊版本不支持自動提交事務的上下文管理器。
您可以升級到最新版本, 創建自己的版本,或者可以在關閉連接之前顯式調用db.commit()
。
此外,也不必(或不希望)為插入的每一行建立與數據庫的新連接並創建新的游標。 您的代碼最好像這樣編寫:
import MySQLdb
import feedparser
myfeed = feedparser.parse("http://www.couponchief.com/rss/")
# get connection and cursor upfront, before entering loop
with MySQLdb.connect('mysql.server','lesmun','*******','lesmun$default') as db:
cur = db.cursor()
for item in myfeed['items']:
title = item.title
link = item.link
description = item.description
print title
print link
print description
cur.execute("INSERT INTO ini (title, link, description) VALUES (%s, %s, %s)", (title, link, description))
print 'Succesfull!'
# db.commit() # for older MySQLdb drivers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.