gpt4 book ai didi

ruby - 如何在ruby中模拟http post请求和解析html响应

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:45 24 4
gpt4 key购买 nike

我正在尝试模拟与此网页上的表单相关的 http 请求: http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do然后我需要解析响应 html。

我正在尝试使用 gem 'webmock' 和 'rest-client'。所以在研究了原始的 http 请求之后,我尝试了类似的东西:

def post
stub_request(:post, "http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do").
with(:body => {"data"=>{"dispatch"=>"filtrer", "hoi_atp"=>"T", "dtdate"=>"30/12/2015", "mois"=>"12", "multi_lig_cod"=>"0", "lig_cno_1"=>"01", "bidf"=>"0", "imit_categ_age"=>"0", "hoiCtmcDames"=>"T"}},
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'174', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})

resp = RestClient.post('http://www.ei.applipub-fft.fr/eipublic/competitionRecherche.do', "data[dispatch]=filtrer&data[hoi_atp]=T&data[dtdate]=30/12/2015&data[mois]=12&data[multi_lig_cod]=0&data[lig_cno_1]=01&data[bidf]=0&data[imit_categ_age]=0&data[hoiCtmcDames]=T",
:content_type => 'application/x-www-form-urlencoded')
puts resp a
end

但我无法获得正确的 html 响应。谁能解释这一切是如何运作的?

最佳答案

我通常会使用 HTTParty gem 来完成。它允许您使用 Ruby 编写发布请求。示例:

HTTParty.post('http://example.com/post_here', 
body: { subject: 'This is the screen name',
description: 'This is the description for the problem'
}.to_json,
headers: { 'Content-Type' => 'application/json' } )

关于ruby - 如何在ruby中模拟http post请求和解析html响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34543120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com