用Python从Twitch抓取截图

2024-10-02 10:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

现在,我正在启动一个Python项目,该项目应该获取所选twitch频道的屏幕截图,修改这些截图并将它们放到GUI上。GUI不应该是个问题,但我的屏幕截图有问题。
我找到了两个资源来处理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'

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

^{pr2}$

creator的示例目录是'./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

任何关于如何让它运行或使用不同资源的想法都将非常感谢。在


Tags: 项目inpy目录脚本屏幕错误line
2条回答

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

你不应该从你试图使用的开源项目中删除一些东西。在

相反,安装丢失的软件包

pip install requests如果您对此有问题,可能您没有{},所以只需安装它。在

或者使用这个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

^{pr2}$

它只是表示您没有安装Imagemagick。您可以通过下载适合您的体系结构的正确安装程序来安装它:http://www.imagemagick.org/script/binary-releases.php

然后在安装时勾选以下选项: enter image description here

然后尝试确保从终端执行convert命令。如果没有,请遵循以下说明:

Lastly you have to set MAGICK_HOME environment variable to the path of ImageMagick (e.g. C:\Program Files\ImageMagick-6.7.7-Q16). You can set it in Computer ‣ Properties ‣ Advanced system settings ‣ Advanced ‣ Environment Variables....

source

相关问题 更多 >

    热门问题