'How to parse a xml feed using feed parser python?
Im trying to parse a feed in python using feedparser. But all I get is None
returned. Im not sure what im missing. Here is my code:
import feedparser
def rss(self):
rss = 'https://news.google.com/news?q=fashion&output=rss'
feed = feedparser.parse(rss)
for key in feed.entries:
return key.title
If you think there is a better rss/xml feed parse. Please let me know. (Im new to python)
print(key)
displays none
and print(len(feed.entries))
also displays none
print(feed)
{'feed': {}, 'entries': [], 'bozo': 1, 'bozo_exception': URLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),)}
print(feedparser)
<module 'feedparser' from '/Users/User_name/python-projects/my_env/lib/python3.6/site-packages/feedparser.py'>
Solution 1:[1]
Figured out the issue was actually with the SSL handshake fixed it by adding ssl._create_default_https_context = ssl._create_unverified_context
.
For anyone else facing the issue. Full code is:
import feedparser
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
rss = 'https://news.google.com/news?q=fashion&output=rss'
feed = feedparser.parse(rss)
print(feed)
Solution 2:[2]
Try the following basic code, which works fine for me and gave me 10 items in the feed when I ran it.
- Install feedparser from pip
pip install feedparser
- Usage
import urllib2
import feedparser
url = "https://news.google.com/news?q=fashion&output=rss"
response = urllib2.urlopen(url).read()
print response
d = feedparser.parse(response)
print len(d.entries)
for item in d.entries:
print "------"
print item.title
if 'subtitle' in item:
print item.subtitle
print item.link
print item.description
print item.published
print item.id
print item.updated
if 'content' in item:
print item.content
Or, paste the FULL code you're running, and I'll take a look.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kevinabraham |
Solution 2 | Ged Flod |