如何从 YouTube 视频中获取一些数据? (Python)
How can I get some data from a video YouTube? (Python)
我想知道如何从 Youtube 视频中获取一些数据,例如观看次数、缩略图或评论。我一直在Google的API里找,但看不懂
谢谢!
我认为这就是您要找的部分 (source):
def get_video_localization(youtube, video_id, language):
results = youtube.videos().list(
part="snippet",
id=video_id,
hl=language
).execute()
localized = results["items"][0]["snippet"]["localized"]
localized
现在将包含标题、描述等
另一种方法是使用 urllib2 并从页面获取 HTML 代码,然后对其进行过滤。
import urllib2
source = 'https://www.youtube.com/watch?v=wDjeBNv6ip0'
response = urllib2.urlopen(source)
html = response.read() #Done, you have the whole HTML file in a gigantic string.
之后,您所要做的就是像过滤字符串一样过滤它。
获取观看次数例如:
wordBreak = ['<','>']
html = list(html)
i = 0
while i < len(html):
if html[i] in wordBreak:
html[i] = ' '
i += 1
#The block above is just to make the html.split() easier.
html = ''.join(html)
html = html.split()
dataSwitch = False
numOfViews = ''
for element in html:
if element == '/div':
dataSwitch = False
if dataSwitch:
numOfViews += str(element)
if element == 'class="watch-view-count"':
dataSwitch = True
print (numOfViews)
>>> 45.608.212 views
这是获取浏览量的简单示例,但您可以对页面上的所有内容执行此操作,包括评论数、点赞数、评论本身的内容等。
我想知道如何从 Youtube 视频中获取一些数据,例如观看次数、缩略图或评论。我一直在Google的API里找,但看不懂
谢谢!
我认为这就是您要找的部分 (source):
def get_video_localization(youtube, video_id, language):
results = youtube.videos().list(
part="snippet",
id=video_id,
hl=language
).execute()
localized = results["items"][0]["snippet"]["localized"]
localized
现在将包含标题、描述等
另一种方法是使用 urllib2 并从页面获取 HTML 代码,然后对其进行过滤。
import urllib2
source = 'https://www.youtube.com/watch?v=wDjeBNv6ip0'
response = urllib2.urlopen(source)
html = response.read() #Done, you have the whole HTML file in a gigantic string.
之后,您所要做的就是像过滤字符串一样过滤它。
获取观看次数例如:
wordBreak = ['<','>']
html = list(html)
i = 0
while i < len(html):
if html[i] in wordBreak:
html[i] = ' '
i += 1
#The block above is just to make the html.split() easier.
html = ''.join(html)
html = html.split()
dataSwitch = False
numOfViews = ''
for element in html:
if element == '/div':
dataSwitch = False
if dataSwitch:
numOfViews += str(element)
if element == 'class="watch-view-count"':
dataSwitch = True
print (numOfViews)
>>> 45.608.212 views
这是获取浏览量的简单示例,但您可以对页面上的所有内容执行此操作,包括评论数、点赞数、评论本身的内容等。