How to consume XML from RESTful web services using Django / Python?

后端 未结 3 1918
感动是毒
感动是毒 2021-02-04 21:20

Should I use PyXML or what\'s in the standard library?

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 22:15

    There's also BeautifulSoup, which has an API some might prefer. Here's an example on how you can extract all tweets that have been favorited from Twitter's Public Timeline:

    from BeautifulSoup import BeautifulStoneSoup
    import urllib
    
    url = urllib.urlopen('http://twitter.com/statuses/public_timeline.xml').read()
    favorited = []
    
    soup = BeautifulStoneSoup(url)
    statuses = soup.findAll('status')
    
    for status in statuses:
        if status.find('favorited').contents != [u'false']:
            favorited.append(status)
    

提交回复
热议问题