2016-03-05 58 views
0

我想從雅虎財經 http://finance.yahoo.com/q/hp?s=^TWII&a=00&b=15&c=2004&d=11&e=4&f=2015&g=mscrapy:如何在空白處輸入參數

我希望程序在啓動一年空白處鍵入「2004」和「2015年」的結束年數據下載空間。我怎樣才能做到這一點?

我的代碼如下所示:

import scrapy 
from selenium import webdriver 
import time 
from scrapy.selector import Selector 
from scrapy.selector import HtmlXPathSelector 
from taiex.items import taiexItem 
import unicodecsv as csv 

class taiex_spider(scrapy.Spider): 
    name = 'taiex_spider' 
    allowed_domains = ['finance.yahoo.com/'] 
    start_urls = ['http://finance.yahoo.com/q/hp?s=^TWII&a=00&b=15&c=2004&d=11&e=4&f=2015&g=m'] 

    def __init__(self): 
     self.driver = webdriver.Firefox() 

    def parse(self, response): 
     items = [] 
     item = taiexItem() 
     driver = self.driver 
     driver.get(response.url) 
     driver.find_element_by_css_selector('select[id="selstart"]>option[value="00"]').click() 
     driver.find_element_by_css_selector('select[id="selend"]>option[value="11"]').click() 

     driver.find_element_by_xpath('//input[@id="monthly"]').click() 
     driver.find_element_by_xpath('//input[@class="rapid-nf"]').click() 
     driver.find_element_by_partial_link_text('Download to Spreadsheet').click() 
+0

你可以只使用scrapy做到這一切,沒有硒,有即使是他們提供的CSV,您可以下載http://stackoverflow.com/questions/35438381/how-to-import-a-table-with-headings-to-a-data-frame-using-pandas-module/35441264 #35441264。還有一個雅虎json apihttp://stackoverflow.com/a/35541497/2141635 –

回答

1

找到由ID所需的輸入元素和發送鍵對他們說:

start_year = driver.find_element_by_id("startyear") 
start_year.clear() 
start_year.send_keys("2004") 

end_year = driver.find_element_by_id("endyear") 
end_year.clear() 
end_year.send_keys("2015") 
+0

謝謝!有效! –