Very new to python. I am trying to get the data from an RSS feed, parse the data and then insert the data to a database. My short bit of code gets the correct items and I can print the results but I can only get the last item in the RSS feed to post to the database. I believe I may have defined "html" and "link" incorrectly. I want the item.title and item.link to populate a list that will then get entered into the database in the correct order. Any help much appreciated.
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()