Issue
I'm trying to create a python function that takes in a youtube url video link and returns the description (if it exists) as a string with all non-ascii characters replaced with a space. I'm having some trouble getting it to work though.
Anyone got any ideas. I think that <p id="eow-description" ></p>
is used in all videos to hold the description but I can't figure out how to have it return just an entire string, formatted how we see it and not how it's formatted in the page source.
I'm using these 2 videos to test it with and without descriptions.
https://www.youtube.com/watch?v=9bZkp7q19f0
https://www.youtube.com/watch?v=eHvccEXfacM
video_source = requests.get("https://www.youtube.com/watch?v=9bZkp7q19f0")
parsed_soup = BeautifulSoup(video_source.content)
print parsed_soup.find_all("p", {"id": "eow-description"})[0]
I don't know how to go about formatting that into a string.
Solution
You'd be much better off using the YouTube Data API, there is a list
endpoint that'll return detailed information for one or more ids per API call.
Grab yourself an API key (see the instructions, for a script on your on computer use a server API key) and the Python client libraries; install these with pip install --upgrade google-api-python-client
.
Then listing descriptions can be done with:
from apiclient.discovery import build
DEVELOPER_KEY = '<API key provided by Google>'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
ids = '9bZkp7q19f0,eHvccEXfacM'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
print result['id']
print result['snippet']['description']
print '-----'
Demo:
>>> from apiclient.discovery import build
>>> DEVELOPER_KEY = '<get your own key here>'
>>> youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
>>> ids = '9bZkp7q19f0,eHvccEXfacM'
>>> results = youtube.videos().list(id=ids, part='snippet').execute()
>>> for result in results.get('items', []):
... print result['id']
... print result['snippet']['description']
... print '-----'
...
9bZkp7q19f0
▶ Watch HANGOVER feat. Snoop Dogg M/V @
http://youtu.be/HkMNOlYcpHg
PSY - Gangnam Style (강남스타일)
▶ Available on iTunes: http://Smarturl.it/psygangnam
▶ Official PSY Online Store US & International : http://psy.shop.bravadousa.com/
▶ About PSY from YG Ent.: http://smarturl.it/YGfamilyAboutPSY
▶ PSY's Products on eBay: http://stores.ebay.com/ygentertainment
▶ YG-eShop: http://www.ygeshop.com
For More Information @
http://www.facebook.com/officialpsy
http://twitter.com/psy_oppa
http://twitter.com/ygent_official
http://me2day.net/psyfive
http://www.psypark.com
App Store: http://goo.gl/l9TU6
Google Play: http://goo.gl/UiEn1
© YG Entertainment Inc. All rights reserved.
-----
eHvccEXfacM
-----
Answered By - Martijn Pieters
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.