【问题标题】:POST Request in Ruby/Python returning 'No Data'Ruby/Python 中的 POST 请求返回“无数据”
【发布时间】:2016-04-02 16:22:23
【问题描述】:

我正在尝试通过 Ruby 或 Python 为以下网站上的表单发出 POST 请求: http://diamond-cut.com.au/holloway_cut_adviser.htm

请求返回“无数据”。我想我在请求标头中省略了某些内容(例如,用户代理、接受。包括这些参数并没有改变结果)

在 Ruby 中返回“无数据”的最少代码:

require 'restclient'
url='http://www.pricescope.com/hca.php'
params = {"depth_textbox" => '60',
          "table_textbox" => '57',
          "crown_listbox" => "0",
          "crown_textbox" => '34',
          "pavilion_listbox" => "0",
          "pavilion_textbox" => '40.5',
          "cutlet_textbox" => "0"}

page=RestClient.post(url,params)

在 Python 中:

import requests
url='http://www.pricescope.com/hca.php'
params = {"depth_textbox" : '60',
          "table_textbox" : '57',
          "crown_listbox" : "0",
          "crown_textbox" : '34',
          "pavilion_listbox" : "0",
          "pavilion_textbox" : '40.5',
          "cutlet_textbox" : "0"}
r=requests.post(url,params)

【问题讨论】:

    标签: python ruby post web-scraping python-requests


    【解决方案1】:

    你需要稍微玩一下标题:

    headers = {'Referer': 'http://www.pricescope.com/hca.php'}
    
    r = requests.post(url, data=params, headers=headers)
    print r.content
    

    【讨论】:

    • 谢谢!有效。为了完整起见,这是 Ruby 中的等效修复:page=RestClient.post(url,data=params,headers={'Referer'=>url}) 是否有直观的解释来解释为什么需要 Referer 标头?另外,调试如何揭示这一点?
    • @GolanTrevize 这是来自网站所有者的限制。
    【解决方案2】:

    在你的python代码中,应该如下。

    r=requests.post(url,data=params)

    这个links 也可以帮助你理解 POST 语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多