我正在努力从youtube频道检索元数据和它的视频。
一切都很顺利,但目前我正在努力将所有信息放入我需要的dataframe中。下面是我从这个github https://gist.github.com/andkamau/0d4e312c97f41a975440a05fd76b1d29中使用的以下代码
import urllib.request
import json
from bs4 import BeautifulSoup
from collections import namedtuple
import pafy
from pandas import *
import pandas as pd
df = pd.DataFrame()
Video = namedtuple("Video", "video_id title duration views thumbnail Description")
def parse_video_div(div):
video_id = div.get("data-context-item-id", "")
title = div.find("a", "yt-uix-tile-link").text
duration = div.find("span", "video-time").contents[0].text
views = str(div.find("ul", "yt-lockup-meta-info").contents[0].text.rstrip(" views").replace(",", ""))
img = div.find("img")
videoDescription = pafy.new("https://www.youtube.com/watch?v="+video_id)
thumbnail = "http:" + img.get("src", "") if img else ""
Description = videoDescription.description
l = Video(video_id, title, duration, views, thumbnail, Description)
# storing in the dataframe
df = pd.DataFrame(list(Video(video_id, title, duration, views, thumbnail, Description)))
return Video(video_id, title, duration, views, thumbnail, Description)
def parse_videos_page(page):
video_divs = page.find_all("div", "yt-lockup-video")
return [parse_video_div(div) for div in video_divs]
def find_load_more_url(page):
for button in page.find_all("button"):
url = button.get("data-uix-load-more-href")
if url:
return "http://www.youtube.com" + url
def download_page(url):
print("Downloading {0}".format(url))
return urllib.request.urlopen(url).read()
def get_videos(username):
page_url = "http://www.youtube.com/channel/{0}/videos".format(username)
page = BeautifulSoup(download_page(page_url))
videos = parse_videos_page(page)
page_url = find_load_more_url(page)
while page_url:
json_data = json.loads(str(download_page(page_url).decode("utf-8")))
page = BeautifulSoup(json_data.get("content_html", ""))
videos.extend(parse_videos_page(page))
page_url = find_load_more_url(BeautifulSoup(json_data.get("load_more_widget_html", "")))
return videos
if __name__ == "__main__":
videos = get_videos("UC-M9eLhclbe16sDaxLzc0ng")
for video in videos:
print(video)
print("{0} videos".format(len(videos)))函数parse_video_div(div)包含所有信息和我的dataframe。但不幸的是,dataframe不会返回任何内容。也许我需要以某种方式循环namedtuple。
关于如何实现dataframe来查看数据,有什么线索吗?
发布于 2018-07-04 20:30:45
pd.DataFrame与namedtuple完美地结合在一起,实际上构造了列。
示例数据:
In [21]: Video = namedtuple("Video", "video_id title duration views thumbnail De
...: scription")
In [22]: In [20]: pd.DataFrame(data=[Video(1, 'Vid Title', 5, 10, 'Thumb',' Des'
...: )])
Out[22]:
video_id title duration views thumbnail Description
0 1 Vid Title 5 10 Thumb Des既然您的函数实际上并没有返回df,并且没有在代码中的其他任何地方使用它,那么您如何确定它是空的呢?
更新
您只需编辑parse_video_div的返回值以返回一个pd.DataFrame,并在get_videos函数中将该列表连接到一个pd.DataFrame中。
以下是突出显示的编辑。
def parse_video_div(div):
#####
return pd.DataFrame(data=[Video(video_id, title, duration, views, thumbnail, Description)])
# shorter version
# return pd.DataFrame(data=[l])
def get_videos(username):
####
videos_df = pd.concat(videos, ignore_index=True)
return videos_df # return the DataFrame最后你需要一个concantenation函数。在parse_page_div中,您可以返回任何pd.DataFrame输入,可以是dict、pd.Series、namedtuple,甚至可以是列表。在本例中,我选择了pd.DataFrame来简化操作,但是,就性能而言,它可以增加几毫秒的处理时间。
https://stackoverflow.com/questions/51173492
复制相似问题