Skip to main content
 首页 » 编程设计

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

2025年04月02日16softidea

现在,我正在启动一个 Python 项目,该项目应该截取选定的抽动 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()