2015-08-21 46 views
0

這是我的代碼。保存硒的快照

import requests, time, csv, urllib2, math, os, datetime 
from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.by import By 
from lxml import html 
from scrapy import Selector as s 

path = "D:\Philip\Snapshot"; mydir = os.path.join(path, datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')); active_directory = os.makedirs(mydir) 

stocks_links = ['http://finviz.com/quote.ashx?t=A&b=1', 
       'http://finviz.com/quote.ashx?t=AA&b=1', 
       'http://finviz.com/quote.ashx?t=AAC&b=1', 
       'http://finviz.com/quote.ashx?t=AAL&b=1', 
       'http://finviz.com/quote.ashx?t=AAMC&b=1', 
       'http://finviz.com/quote.ashx?t=AAME&b=1', 
       'http://finviz.com/quote.ashx?t=A&b=1'] 

driver = webdriver.Chrome() 
driver.set_page_load_timeout(30) 

def get_stock_symbols(links): 
    get3 = requests.get(each) 
    source3 = s(text=get3.text,type="html") 
    Stock_symbol = source3.xpath('//td[@align="center"]//a[@id="ticker"]/text()').extract() 
    Stock_symbol = str(Stock_symbol).replace("u'","").replace("'","") 
    print Stock_symbol 
    try: 
    driver.get(each) 
    except Exception: 
     pass 
    driver.maximize_window() 
    driver.execute_script("document.body.style.zoom='90%'") 
    driver.execute_script("window.scrollTo(0, 160)") 
    time.sleep(2) 
    driver.save_screenshot(str(active_directory)+"/"+Stock_symbol+'.jpg') 
    time.sleep(2) 
    driver.execute_script("document.body.style.zoom='100%'") 

for each in stocks_links: 
    get_stock_symbols(each) 

我執行代碼後,我沒有看到任何錯誤,但我的屏幕截圖沒有保存在目錄中。在OS模塊的幫助下,我做了一個目錄,但無法看到那裏的圖像。請幫忙

回答

0

我知道它使用 - os.path.join