【问题标题】:scrapy: how to type argument in a blank spacescrapy:如何在空白处输入参数
【发布时间】:2016-06-18 23:34:48
【问题描述】:

我想从雅虎财经下载数据 http://finance.yahoo.com/q/hp?s=^TWII&a=00&b=15&c=2004&d=11&e=4&f=2015&g=m

我希望程序在开始年份空间中键入“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()

【问题讨论】:

  • 您可以在不使用 selenium 的情况下仅使用 scrapy 完成所有这些操作,甚至还有他们提供的 CSV,您可以下载 stackoverflow.com/questions/35438381/…。还有一个yahoo json apihttp://stackoverflow.com/a/35541497/2141635

标签: python-2.7 selenium web-scraping scrapy web-crawler


【解决方案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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2020-06-27
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多