Python 使用BeautifulSoup获取youtube描述?

Python 使用BeautifulSoup获取youtube描述?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在尝试创建一个python函数,该函数接收youtube url视频链接,并返回描述(如果它是一个字符串,所有非ascii字符都替换为空格)。不过,我在让它工作时遇到了一些麻烦 有人有什么想法吗。我认为在所有视频中都用于保存描述,但我不知道如何让它返回整个字符串,以我们看到的方式格式化,而不是在页面源代码中如何格式化 我用这两个视频来测试它有没有描述。 我不知道如何将其格式化为字符串。Beautifulsoup非常慢,最好的方法是按照注释中的建议使用。这要简单得多: def PrintEn

我正在尝试创建一个python函数,该函数接收youtube url视频链接,并返回描述(如果它是一个字符串,所有非ascii字符都替换为空格)。不过,我在让它工作时遇到了一些麻烦

有人有什么想法吗。我认为

在所有视频中都用于保存描述,但我不知道如何让它返回整个字符串,以我们看到的方式格式化,而不是在页面源代码中如何格式化

我用这两个视频来测试它有没有描述。


我不知道如何将其格式化为字符串。

Beautifulsoup非常慢,最好的方法是按照注释中的建议使用。这要简单得多:

def PrintEntryDetails(entry):
    print 'Video description: %s' % entry.media.description.text

不是您想要的,请告诉我您最好使用,有一个列表端点,它将为每个API调用返回一个或多个ID的详细信息

为自己获取一个API密钥,请参阅,对于计算机上的脚本,请使用服务器API密钥和;使用pip安装-升级google api python客户端安装这些

然后,可以通过以下方式完成清单说明:

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 '-----'
演示:


到目前为止,你能给我们看一下你的代码吗?你考虑过使用吗?它允许您使用/list端点一次获取多个视频的信息,返回的信息包括描述。否则,请向我们展示您的实际代码,也许您有错误?@MartijnPieters您能检查一下我是否做对了:@MohitBhasi:您做得不对,很抱歉。对于您链接的说明中的步骤5,我该怎么做?@Johnny使用API密钥;我的代码名为it开发者\ U密钥。@Johnny:对不起;我已经有了一个API密钥,很早就忘记了有不同的类型。我会使用服务器API键来创建脚本。
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 '-----'
>>> 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

-----