【问题标题】:Python - Requests: Correctly Using Params?Python - 请求:正确使用参数?
【发布时间】:2015-02-26 09:41:40
【问题描述】:

在开始之前,我只想说,我对通过代码与 Web 进行一般通信非常陌生。话虽如此,谁能帮我获取这些参数,

        'a': stMonth,
        'b': stDate,
        'c': stYear,
        'd': enMonth,
        'e': enDate,
        'f': enYear,
        'submit': 'submit'

用于此页面上的“设置日期范围”框, http://finance.yahoo.com/q/hp?s=gspc&a=00&b=3&c=1951&d=11&e=29&f=2014&g=d&z=66&y=0

,在我的 Python 代码中工作。它目前包括:

def getHistoricData(symbol, stMonth, stDate, stYear, enMonth, enDate, enYear):  
    url = 'http://finance.yahoo.com/q/hp?s=%s&a=00&b=3&c=1951&d=11&e=29&f=2014&g=d&z=66&y=0' % symbol    
    params = {
        'a': stMonth,
        'b': stDate,
        'c': stYear,
        'd': enMonth,
        'e': enDate,
        'f': enYear,
        'submit': 'submit',
    }  
    response = requests.get(url, params=params)  
    tree = html.document_fromstring(response.content)

symbol = raw_input("Symbol: ")
getHistoricData(symbol, '00', '11', '2010', '00', '13', '2010')

我认为参数的名称或值可能有问题,但我不能确定。在此先感谢 - 非常感谢任何和所有帮助! (包括批评,只要它至少有点建设性!)

【问题讨论】:

    标签: python html request lxml lxml.html


    【解决方案1】:

    具有name 属性的<input> 元素等于:

    a, b, c, d, e, f, g(the radio button Daily/Weekly/Monthly)
    

    位于<form> 标签内,其中包含hidden form field

    <input type="hidden" name="s" value="^GSPC" data-rapid_p="11">
    

    这将向服务器发送一个名称/值对,就像普通的&lt;input&gt; 元素一样。 您需要在请求中包含该名称/值对,以便服务器端程序知道您请求的数据是哪只股票。

    表单中的submit button 也会向服务器发送一个名称/值对,但它并不重要,在这种情况下你可以省略它:

    import requests
    
    url = 'http://finance.yahoo.com/q/hp'
    
    params = {
        's': '^GSPC', #<input type="hidden" name="s" value="^GSPC" data-rapid_p="11">
        'a': 1, #stMonth,
        'b': 16, #stDate,
        'c': 2014, #stYear,
        'd': 1, #enMonth,
        'e': 18, #enDate,
        'f': 2014, #enYear,
        'g': 'd', #daily/weekly/monthly
    }  
    
    resp = requests.get(url, params=params) 
    print resp.text
    print resp.url
    

    resp.url 实际上是 request 被发送到的 url,您可以通过打印它来检查它:

    http://finance.yahoo.com/q/hp?a=1&c=2014&b=16&e=18&d=1&g=d&f=2014&s=%5EGSPC
    

    如果您将其复制到浏览器的地址栏中,您将看到结果。 resp.text 是包含您的结果的页面的 html 标记。您必须知道如何搜索 html 才能找到具体的结果。要使用 python 搜索 html,请查看:

    1. 美汤
    2. lxml

    【讨论】:

      【解决方案2】:

      我认为您不需要使用参数。简单地格式化一个 URL 就足够了。像这样:

      # -*- coding: utf-8 -*-
      #!/usr/bin/python
      
      import requests
      
      symbol = raw_input("Symbol: ")
      params = (symbol, '00', '11', '2010', '00', '13', '2010')
      
      url = 'http://finance.yahoo.com/q/hp?s=%s&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s&g=d' % params     
      response = requests.get(url)
      # you will get 200 OK here 
      print response
      # and page info is in response.text
      

      【讨论】:

        【解决方案3】:

        您不需要submit 参数,但需要g。这里d 表示daily

        def getHistoricData(symbol, stMonth, stDate, stYear, enMonth, enDate, enYear):
            url = 'http://finance.yahoo.com/q/hp'
            params = {
                's': symbol,
                'a': stMonth,
                'b': stDate,
                'c': stYear,
                'd': enMonth,
                'e': enDate,
                'f': enYear,
                'g': 'd'
            }  
            response = requests.get(url, params=params)  
            tree = html.document_fromstring(response.content)
            print tree.xpath('.//table[@class="yfnc_datamodoutline1"]//tr/td[1]/text()')
        

        例如,如果您调用:

        getHistoricData('^GSPC', '02', '3', '1950', '10', '30', '2014')
        

        打印以下内容(日期来自第一列):

        [
            'Nov 28, 2014', 
            'Nov 26, 2014', 
            'Nov 25, 2014', 
            'Nov 24, 2014',
            ...
        ]
        

        【讨论】:

          猜你喜欢
          • 2012-08-19
          • 1970-01-01
          • 2021-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-04
          • 2019-02-19
          相关资源
          最近更新 更多