2016-06-14 78 views
2

現在,我開始一個Python項目,該項目應該對選定的twitch頻道進行截圖,修改這些截圖並將其放在GUI上。圖形用戶界面不應該是一個問題,但我的屏幕截圖有問題。
我發現了2個資源來處理twitch通信:python-twitch包和一個名爲ttvsnap的腳本(https://github.com/chfoo/ttvsnap)。
這個軟件包對我沒有任何幫助,因爲我沒有找到任何與屏幕截圖相關的東西。該腳本看起來很有前途,但是我遇到了一些問題:使用Python從Twitch抓取屏幕截圖

根據創建者的說法,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/」,但與投入,我發現了以下錯誤(也許是因爲我」 M於Windows)中:

Output directory specified is not valid. 

試圖像C目錄:\ DevFiles \截圖給我下面的錯誤:

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 

如何得到它運行任何想法或不同的資源使用將非常感激。

+1

你有沒有試過'PIP安裝requests'?它不是標準庫的一部分。 – jonrsharpe

回答

0

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

爲我的目的,是否有任何硒(如ImageMagick)必需品或我安裝包後我很好去? – Hillburn

+0

更新了帖子,爲您舉例。 –

+0

謝謝,這個例子適用於PhantomJS。但是,我是Python的新手,所以我不完全瞭解如何設置屏幕截圖的目標。 「__ file __」變量引用的是包含腳本本身的文件嗎? – Hillburn

1

您不應該從您嘗試使用的開源項目中刪除項目。

相反,安裝缺少的包,

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做。您可以通過下載正確安裝你的架構在這裏安裝: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

+0

已安裝的請求,現在我不必再從腳本中刪除它了,但我仍然收到與之前一樣的錯誤消息 – Hillburn

+0

@Hillburn您是否創建了該文件夾?它目前是否存在? – majidarif

+0

@Hillburn嘗試使用'python3 ttvsnap.py verycoolstreamer ./ screenshots /',但要確保運行腳本的當前目錄中存在「screenshots」目錄。 – majidarif