python - 使用 Python 从 Twitch 抓取屏幕截图

标签 python screenshot python-3.5 twitch

现在,我正在启动一个 Python 项目,该项目应该对选定的 twitch channel 进行屏幕截图,修改这些屏幕截图并将它们放在 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

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

最佳答案

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()

关于python - 使用 Python 从 Twitch 抓取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815613/

相关文章:

python - pandas dataframe.apply -- 将十六进制字符串转换为整数

python - 使用 interp1d 在数据帧中按行插值

c++ - 截图 C++ directx 获取黑色图像

ios - UIPickerView 在屏幕截图上变暗

python-3.5 - 使用python从PC麦克风获取音频输入

python - 在 Qt 应用程序中包含 Python.h 会导致对 Qt 函数的 undefined reference

python - 绘图方法需要返回吗?

delphi - 在lazarus或delphi中调整图像大小?

python - 无法将字符串转换为 int

python - 在 flask 中开始 celery : AttributeError: 'Flask' object has no attribute 'user_options'