【问题标题】:Using TIdHTTP in Delphi to submit form and retrive data (not the usual)在 Delphi 中使用 TIdHTTP 提交表单和检索数据(不是通常的)
【发布时间】:2014-02-10 22:15:24
【问题描述】:

存在一个网页 (www.someurl.co.uk),其中包含一个包含输入字段和图像的表单。

在浏览器中,当用户在输入字段中输入字符串并单击图像时,会提交表单并生成另一个页面 (www.someurl.co.uk/results.php),其中包含以下内容:包含一些结果的表格。

我需要 Delphi 在第一页的表单中填写字符串,提交表单并从结果页面的表格中取回几个单元格的内容。

我使用 TIdHTTP 组件使用下面的代码将数据发布到表单,但之后我在备注框中看到的文本只是初始页面 (www.someurl.co.uk) 的 html。

请有人帮助我使用正确的代码从结果页面获取值 BlaBla_A、BlaBla_C、BlaBla_C 和 BlaBla_D - 并知道哪个是哪个?

(虽然我对Delphi很满意,但我对网页和HTML不太了解,这是我第一次使用Indy组件)

这是我目前正在使用的代码

procedure TForm1.Button2Click(Sender: TObject);
var
  Response: TStringStream;
  Params: TStringList;
begin
Params := TStringList.Create;
try
  Params.Add('thedata=' + 'MyString');
  Response := TStringStream.Create('');
  try
     IdHTTP1.Post('www.someurl.co.uk', Params, Response);
     memo1.Text := Response.DataString;
  except
   on E: Exception do
   begin
    showmessage('Error: ' + E.Message);
   end;
  end;

finally
  Params.Free;
  Response.Free;
end;

end;

数据输入/提交页面(www.someurl.co.uk)的重要部分如下所示

</div>
<h3>Enter the data you want to look for then click FIND</h3>
<form action="results.php" method="post" name="form1" target="_parent" 
      id="form1" 
      onsubmit="if (document.getElementById('text').value==''  
                   || document.getElementById('text').value=='ENTER DATA') 
                   {alert ('Please enter data first then click FIND'); 
                   return false;} 
            else 
               return true;">

<input name="thedata" type="text" id="text" value="ENTER DATA" 
       onclick="effectClear(this.id);this.value = '';" 
       class="validate[required]"/>

<input type="image" src="../images/find.png" name="image" id="homebutton" />
</form>
</div>  

结果页面的重要部分(www.someurl.co.uk/results.php)看起来像这样

<div id="results_details">
    <table id="results_details_table" border="0">
        <tr>
          <td class="result_general"><h2>Here are your results</h2><br/></td>
          <td class="result_info">
              <label>Result_A</label><br/>BlaBla_A<br/>
              <label>Result_B</label><br/>BlaBla_B<br/>
          </td>
          <td class="result_info">
              <label>Result_C</label><br/>BlaBla_C<br/><br/>
              <label>Result_D</label><br/>BlaBla_D<br/><br/>
          </td>
        </tr>
        </table>
</div>

【问题讨论】:

  • 顺便说一句,我已经使用 TwebBrowser 来执行此操作,但它需要几页代码,运行速度非常慢,需要我进行大量字符串搜索,处理结果页面的原始 html 文本.这也是不可靠的,因为有时在我开始解析文本之前结果页面并不总是完全加载。使用 Indy 承诺会更快、更整洁。

标签: html delphi indy


【解决方案1】:

您将数据发布到此 URL,这是错误的:

IdHTTP1.Post('www.someurl.co.uk', Params, Response);

这就是为什么您要再次返回初始页面 - 这就是您实际要求的!

您需要改为发布到此 URL:

IdHTTP1.Post('http://www.someurl.co.uk/results.php', Params, Response);

顺便说一句,你根本不需要TStringStream,Post() 可以为你返回字符串数据:

procedure TForm1.Button2Click(Sender: TObject);
var
  Params: TStringList;
begin
  Params := TStringList.Create;
  try
    Params.Add('thedata=' + 'MyString');
    try
      memo1.Text := IdHTTP1.Post('www.someurl.co.uk', Params);
    except
      on E: Exception do
      begin
        ShowMessage('Error: ' + E.Message);
      end;
    end;
  finally
    Params.Free;
  end;
end;

【讨论】:

  • 太棒了,谢谢。这让我得到了正确的文本——而且速度很快!现在,除了使用大量 pos(s, SomeStringInTheHtml) 之外,是否有一种简单/快速的方法来提取我想要的表数据,即处理返回的 html 就好像它是一个大字符串?
  • 如果 HTML 实际上是 XHTML,那么您可以使用 XML 解析器。否则,有可用的第三方 HTML 解析器。或者只是自己解析HTML,没有错。
  • 不,我猜你是对的。毕竟,HTML 解析器本质上必须只是进行字符串搜索。滚动我自己的解析器甚至可以给我更多的控制权。已采纳答案,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多