现在,我正在启动一个Python项目,该项目应该对选定的抽搐通道进行截屏,然后修改这些截屏并将其放置在GUI上。 GUI应该不成问题,但是屏幕截图有问题。
我发现了2种用于处理抽动通信的资源:python-twitch软件包和一个名为ttvsnap(https://github.com/chfoo/ttvsnap)的脚本。
该软件包对我没有帮助,因为我没有找到任何与屏幕截图有关的内容。该脚本看起来很有希望,但是我遇到了一些问题:
根据创建者的说法,ttvsnap会定期获取抽搐流的屏幕截图,并将其放置在选定的目录中。
如果我尝试启动脚本,则出现此错误:
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()