【问题标题】:Iterating based on list of strings in Python基于Python中的字符串列表进行迭代
【发布时间】:2013-04-22 09:51:45
【问题描述】:

通过文本文件中的 YouTube 视频 ID 列表,下面的代码旨在循环遍历这些视频 ID,同时从所有这些视频中获取评论源。谁能发现我必须犯但找不到的循环错误?

# Set the videoID list
f = open('video_ids.txt', 'r')
videoID_list = f.read().splitlines()
f.close()

# Cycle through videoID list getting comments via the YouTube API
for video_id in videoID_list:
#Define the comments generator
def comments_generator(yt_service, video_id):
    comment_feed = yt_service.GetYouTubeVideoCommentFeed(video_id=video_id)
    while comment_feed is not None:
        for comment in comment_feed.entry:
            yield comment
        next_link = comment_feed.GetNextLink()
        if next_link is None:
            comment_feed = None
        else:
            comment_feed = yt_service.GetYouTubeVideoCommentFeed(next_link.href)

        for comment in comments_generator(yt_service, video_id):

            # About the video
            video_title = entry.media.title.text
            video_date = entry.published.text

            # About comments
            author_name = comment.author[0].name.text
            raw_text = comment.content.text 
            comment_date = comment.published.text

            # Keep only alphanumeric characters and spaces in the comment text
            text = re.sub(r'\W+', ' ', raw_text)

            # Write to a file ('a' means append) - Comment text is set to lowercase [.lower()]
            f = open('video_comments.tsv', 'a')
            f.write("{}\t{}\t{}\t{}\t{}\t{}\t\r".format(video_title, video_date[:10], comment_date[:10], comment_date[11:19], author_name, text.lower()))

            # Also print results on screen - Comment text is set to lowercase [.lower()]
    print("{}\t{}\t{}\t{}\t{}\t{}\t\r".format(video_title, video_date[:10], comment_date[:10], comment_date[11:19], author_name, text.lower()))

【问题讨论】:

  • 有什么问题?预期的输入和输出是多少?你有回溯吗?您提供的信息越多,就越容易提供帮助,也会有更多人有兴趣尝试。
  • 我正在尝试获取代码以获取 video_ids.txt 文件中所有 videoID 的 cmets。但是在只为 txt 中的第一个视频获取 cmets 后,代码停止了。希望能澄清事情。

标签: python api loops youtube


【解决方案1】:

修复代码中的一些错误后:

import gdata.youtube
import gdata.youtube.service
import re

yt_service = gdata.youtube.service.YouTubeService()

# Set the videoID list
f = open('video_ids.txt', 'r')
videoID_list = f.read().splitlines()
f.close()

#Define the comments generator
def comments_generator(yt_service, video_id):
  comment_feed = yt_service.GetYouTubeVideoCommentFeed(video_id=video_id)
  while comment_feed is not None:
    for comment in comment_feed.entry:
      yield comment
    next_link = comment_feed.GetNextLink()
    if next_link is None:
      comment_feed = None
    else:
      comment_feed = yt_service.GetYouTubeVideoCommentFeed(next_link.href)

f = open('video_comments.tsv', 'a')

# Cycle through videoID list getting comments via the YouTube API
for video_id in videoID_list:

  for comment in comments_generator(yt_service, video_id):

    video_entry = yt_service.GetYouTubeVideoEntry(video_id=video_id)

    # About the video
    video_title = video_entry.title.text
    video_date = video_entry.published.text
    # About comments
    author_name = comment.author[0].name.text
    raw_text = comment.content.text
    comment_date = comment.published.text

    # Keep only alphanumeric characters and spaces in the comment text
    text = re.sub(r'\W+', ' ', raw_text)
    # Write to a file ('a' means append) - Comment text is set to lowercase [.lower()]

    f.write("{}\t{}\t{}\t{}\t{}\t{}\t\r".format(video_title, video_date[:10], comment_date[:10], comment_date[11:19], author_name, text.lower()))


    # Also print results on screen - Comment text is set to lowercase [.lower()]
f.close()
print("{}\t{}\t{}\t{}\t{}\t{}\t\r".format(video_title, video_date[:10], comment_date[:10], comment_date[11:19], author_name, text.lower()))

【讨论】:

  • 谢谢mekegi!但是,我正在尝试获取代码以获取 video_ids.txt 文件中所有 videoID 的 cmets。在为文本文件中的第一个视频提取 cmets 后,您建议的版本仍然停止。有什么想法吗?
  • f = open('video_cmets.tsv', 'a') inside "for" 坏主意。在 cicles 之前打开文件并在 cicles 之后关闭文件。我编辑了代码,再试一次
  • 感谢您的帮助!但是很抱歉。仍然只获得第一个视频的 cmets。对我来说,'for video_id in videoID_list' 会读取该文本文件中的每个 videoID,然后重复获取所有 cmets 的例程,并将其附加到输出文件中,这似乎是合理的。但是没有成功...
  • 在 video_ids.txt 中每个 video_id 需要换行样本 "xxxxx\nyyyyy"
  • 我现在可以使用它了。不知何故,问题似乎在于同时获得“video_entry”。和“评论”。来自 API 的数据。如果只选择一种类型,则循环将按需要工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2011-05-01
  • 2012-05-28
  • 1970-01-01
  • 2016-06-27
相关资源
最近更新 更多