代码之家  ›  专栏  ›  技术社区  ›  kevinabraham

如何使用提要解析器python解析xml提要?

  •  2
  • kevinabraham  · 技术社区  · 7 年前

    None 返回。我不确定我错过了什么。这是我的代码:

    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
    

    print(key) 显示器 none print(len(feed.entries)) 没有一个

    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'>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   kevinabraham    7 年前

    发现问题实际上是SSL握手通过添加 ssl._create_default_https_context = ssl._create_unverified_context .

    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)
    
        2
  •  1
  •   Ged Flod    2 年前

    试试下面的基本代码,它对我来说很好,当我运行它时,在提要中给了我10个条目。

    1. feedparser 来自pip
    pip install feedparser
    
    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
    

    或者,粘贴您正在运行的完整代码,我来看看。