Python 仅向postgres数据库输入最后一个值的脚本

Python 仅向postgres数据库输入最后一个值的脚本,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,对python来说非常陌生。我试图从RSS提要获取数据,解析数据,然后将数据插入数据库。我的一小段代码得到了正确的项目,我可以打印结果,但我只能得到RSS提要中要发布到数据库的最后一个项目。我相信我可能错误地定义了“html”和“链接”。我希望item.title和item.link填充一个列表,然后以正确的顺序输入数据库。非常感谢您的帮助 import sys import requests import urllib2 import feedparser import psycopg2 im

对python来说非常陌生。我试图从RSS提要获取数据,解析数据,然后将数据插入数据库。我的一小段代码得到了正确的项目,我可以打印结果,但我只能得到RSS提要中要发布到数据库的最后一个项目。我相信我可能错误地定义了“html”和“链接”。我希望item.title和item.link填充一个列表,然后以正确的顺序输入数据库。非常感谢您的帮助

import sys
import requests
import urllib2
import feedparser
import psycopg2
import psycopg2.extras
from psycopg2.extras import execute_values
import time

url = "https://www.ferc.gov/xml/whats-new.xml"
response = urllib2.urlopen(url).read()


#saving the xml file
response = requests.get(url)
#with open('whats_hot.xml', 'wb') as file:
#    file.write(response.content)


d = feedparser.parse('https://www.ferc.gov/xml/whats-new.xml')


for item in d.entries:
    print "------"
    print item.published
    print item.title
    print item.link
    html = item.published + item.title
    link = item.link



con = psycopg2.connect(database="xx", 
user="xx", password="xx", host="127.0.0.1", 
port="5432")  
print("Database opened successfully")

cur = con.cursor()



#try:
psycopg2.extras.execute_values(cur,
"insert into ferc_hots (link,html) values %s",
      [(link,html)])
#except psycopg2.IntegrityError:
#     print 'Duplicate values found.  Insert was not successful'

con.commit()  
print("Records inserted successfully")  
con.close()

insert语句也需要在for循环中。否则,只插入最后一条记录

con = psycopg2.connect(database="xx", 
user="xx", password="xx", host="127.0.0.1", 
port="5432")  
print("Database opened successfully")

cur = con.cursor()

for item in d.entries:
    print "------"
    print item.published
    print item.title
    print item.link
    html = item.published + item.title
    link = item.link
    psycopg2.extras.execute_values(cur,"insert into ferc_hots (link,html) values %s",[(link,html)])

con.commit()  
print("Records inserted successfully")  
con.close()

另一个选项是保存记录列表,并将它们一起插入末尾。

执行值的参数列表需要是“序列序列”。这是一个列表列表(或元组列表)。在您的情况下,您只在
html
link
中保留最终项目值,因此只提供单个项目

你会想要这样的东西:

args = []
for item in d.entries:
    print "------"
    print item.published
    print item.title
    print item.link
    args.append([item.published + item.title, item.link])
或者,一举:

args = [[item.published + item.title, item.link] for item in d.entries]
然后插入类似于:

psycopg2.extras.execute_values(cur,
          "insert into ferc_hots (link,html) values %s",
          args)

html和link只是单个字符串值。它们在您的循环中不断变化,但当您转到insert时,它们只具有给定的最后一个值。您需要保留一个值元组列表,这些值元组将传递给insert。注意,双括号是因为元组被附加到值列表中。元组的定义类似于(item,item),因此您要追加(item,item),而不仅仅是item

values = []
for item in d.entries:
    print "------"
    print item.published
    print item.title
    print item.link
    values.append((item.link, item.published + item.title))


link
html
在每次执行循环时都会被覆盖。您只在循环终止后进行插入,因此它将使用
link
html
的最后值进行插入。您通常插入多少个链接?另外,如果要填充列表,为什么不在列表中附加
html
link
?至于插入本身,请看一看。这很有效!我最初制作了两个空列表,然后将项目添加到其中。然后我尝试将它们插入数据库,但出现了语法问题。创建一个列表是否有效,因为追加会添加两个由逗号分隔的值?因此,当insert命令列出两个表项时,列表将用相应的值填充该表?execute_values函数正在查找元组列表。元组需要像insert一样对其项进行排序。因此,Insert into table1(colA,colB)值%s需要一个元组列表,如(ColAValue,ColBValue)。元组中可以有任意多个值,它们只需与Insert语句相对应即可。然后,psycopg2模块将确保它们以正确的值(ColAValue、ColBValue)格式结束。谢谢您的回答。我认为一次性的选择是一个列表理解?正确。非常“pythonic”,但需要一些时间来适应。
psycopg2.extras.execute_values(cur,
    "insert into ferc_hots (link,html) values %s",
        values)