Reputation: 623
How would I get the url with the most views in youtube? Here is the link I am using, "http://gdata.youtube.com/feeds/api/videos?q=gangnam%20style"
It returns xml data, how would I get certain elements of this? is their any module I could use to return the gdata link to go to the most viewed video? any help would be appreciated thank you. My problem is I do not know how to get certain elements is why I'm asking and thank you ahead of time.
Edit: Thank you everyone that answerd after about an hour or so on google I found a good way to do it but I appreicate all of you suggestions, but in a few hours when I can answer my own question I'll post it
Upvotes: 0
Views: 192
Reputation: 369064
Use lxml
.
For example, following code prints titles, view counts:
import lxml.etree
tree = lxml.etree.parse('http://gdata.youtube.com/feeds/api/videos?q=gangnam%20style')
root = tree.getroot()
nsmap = root.nsmap
nsmap['xmlns'] = nsmap.pop(None)
for entry in root.findall('.//xmlns:entry', namespaces=nsmap):
title = entry.find('xmlns:title', namespaces=nsmap).text
view_count = entry.find('yt:statistics', namespaces=nsmap).get('viewCount')
print(u'{} {}'.format(title, view_count))
Upvotes: 1
Reputation: 816
Python contains several xml parsers. Take a look at the python standard library documentation. If you're using python2 that's here: http://docs.python.org/2/library/xml.html
The various parsers differ in their complexity, so experiment with each find the one that suites your taste. I prefer elementtree for a simple xml structre. See the tutorial here: http://docs.python.org/2/library/xml.etree.elementtree.html#tutorial
Upvotes: 0
Reputation: 3078
You will want to look at libraries that parse XML for you to retrieve the node you want.
Take a look at
http://docs.python.org/2/library/xml.etree.elementtree.html
It starts off with a good example on how to do this. (country_data.xml)
Upvotes: 0