Search code examples
pythonxmlrssfeedparser

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

  • 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)