【问题标题】:Grabbing a screenshot from Twitch with Python使用 Python 从 Twitch 抓取屏幕截图
【发布时间】:2016-06-14 14:59:22
【问题描述】:

现在,我正在启动一个 Python 项目,该项目应该对选定的 twitch 频道进行屏幕截图,修改这些屏幕截图并将它们放在 GUI 上。 GUI 应该不是问题,但我的屏幕截图有问题。
我找到了 2 个资源来处理 twitch 通信:python-twitch 包和一个名为 ttvsnap (https://github.com/chfoo/ttvsnap) 的脚本。
这个包对我没有帮助,因为我没有找到任何与截图相关的东西。脚本看起来很有希望,但我遇到了一些问题:

根据创建者的说法,ttvsnap 会定期截取 twitch 流的屏幕截图并将它们放在选定的目录中。
如果我尝试启动脚本,我会收到以下错误:

Traceback (most recent call last):  
    File "ttvsnap.py", line 13, in <module>
        import requests  
ImportError: No module named 'requests'

从脚本中删除“导入请求”允许我运行它,但是脚本在选择目录时出现问题。要运行脚本,我应该写:

Python ttvsnap.py 'streamname here' 'directory here'

来自创建者的示例目录是“./screenshot/”,但使用该输入,我收到以下错误(可能是因为我在 Windows 上?):

Output directory specified is not valid.

尝试像 C:\DevFiles\Screenshots 这样的目录会出现以下错误:

Invalid drive specification. ###Translated this line since I'm using a German OS
Traceback (most recent call last):
  File "ttvsnap.py", line 176, in <module>
    main()
  File "ttvsnap.py", line 46, in main
    subprocess.check_call(['convert', '-version'])
  File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 584, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['convert', '-version']' returned non-zero exit status 4

任何关于如何让它运行或使用不同资源的想法将不胜感激。

【问题讨论】:

  • 你试过pip install requests吗?它不是标准库的一部分。

标签: python screenshot python-3.5 twitch


【解决方案1】:

Selenium 可以方便地浏览网站和截取屏幕截图。

http://selenium-python.readthedocs.io/

充实了一个可以满足您需求的示例。 要点链接: https://gist.github.com/ryantownshend/6449c4d78793f015f3adda22a46f1a19

"""
basic example.

Dirt simple example of using selenium to screenshot a site.

Defaults to using local Firefox install.
Can be setup to use PhantomJS

http://phantomjs.org/download.html

This example will run in both python 2 and 3
"""
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def main():
    """the main function."""
    driver = webdriver.Firefox()
    # driver = webdriver.PhantomJS()
    driver.get("http://google.com")
    # assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.clear()
    elem.send_keys("cats")
    elem.send_keys(Keys.RETURN)

    # give the query result time to load
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "resultStats"))
    )

    # take the screenshot
    pwd = os.path.dirname(os.path.realpath(__file__))
    driver.save_screenshot(os.path.join(pwd, 'cats.png'))
    driver.close()

if __name__ == '__main__':
    main()

【讨论】:

  • 就我的目的而言,selenium 是否有任何必要条件(如 ImageMagick),或者安装软件包后是否可以使用?
  • 更新了帖子,给你一个例子。
  • 谢谢,这个例子适用于我的 PhantomJS。但是,我是 Python 新手,所以我不完全了解您如何设置屏幕截图的目标。 “__ file __”变量是指包含您的脚本本身的文件吗?
  • 正是如此。然后从那里你可以使用 os.path 库来构建一个有效的本地路径。
【解决方案2】:

您不应该从您尝试使用的开源项目中删除任何东西。

请安装缺少的软件包,

pip install requests 如果您对此有疑问,也许您没有pip,所以只需安装它。

或者使用这个python.exe -m pip install requests


这个错误Output directory specified is not valid.是由于这一行:

if not os.path.isdir(args.output_dir):
    sys.exit('Output directory specified is not valid.')

一般表示目录不存在


至于最后一个错误,无法执行命令convert

Invalid drive specification. ###Translated this line since I'm using a German OS
Traceback (most recent call last):
  File "ttvsnap.py", line 176, in <module>
    main()
  File "ttvsnap.py", line 46, in main
    subprocess.check_call(['convert', '-version'])
  File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 584, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['convert', '-version']' returned non-zero exit status 4

这只是意味着您没有安装Imagemagick。您可以在此处下载适合您架构的正确安装程序来安装它:Link

然后勾选这些选项进行安装:

然后尝试确保convert 命令从您的终端执行。如果没有,请按照以下说明操作:

最后你必须将 MAGICK_HOME 环境变量设置为 ImageMagick(例如 C:\Program Files\ImageMagick-6.7.7-Q16)。你可以设置 它在计算机‣属性‣高级系统设置‣高级‣ 环境变量....

source

【讨论】:

  • 已安装的请求,现在我不必再从脚本中删除它,但我仍然收到与以前相同的错误消息
  • @Hillburn 您是否创建了相关文件夹?目前是否存在?
  • @Hillburn 试试python3 ttvsnap.py verycoolstreamer ./screenshots/,但要确保screenshots 存在于运行脚本的当前目录中。
  • 我写了 'Python ttvsnap.py SaltyBet ./screens/' 并在 ttvsnap 文件夹内有一个名为 screen 的文件夹,但我仍然收到我在问题中发布的最后一个错误。
  • @Hillburn 我更新了我的答案。这是因为您没有安装ImageMagick
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2011-01-12
相关资源
最近更新 更多