2

There exists a web page (www.someurl.co.uk) that, amongst other stuff, has inside it a form containing an input field and an image.

In a browser, when the user enters a string in the input field and clicks the image, the form is submitted and another page is generated (www.someurl.co.uk/results.php) containing, amongst other things, a table containing some results.

I need Delphi to fill in the string in the form on the first page, submit the form and get back the contents of a couple of cells from the table on the results page.

I have used the TIdHTTP component to post data to the form using the code below but the text I see in the memo box afterwards is simply the html of the initial page (www.someurl.co.uk).

Please can somebody help me with the correct code to get the the values BlaBla_A, BlaBla_C, BlaBla_C and BlaBla_D from the results page - and to know which is which?

(Although I'm happy with Delphi, I don't know much about web pages and HTML and this is my first time using the Indy component)

This is the code I am using at the moment

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;

The important bit of the data entry/submit page (www.someurl.co.uk) looks like this

</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>  

and the important bit of the results page (www.someurl.co.uk/results.php) looks like this

<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>
1
  • 1
    BTW I have used TwebBrowser to do this and that works, but it took several pages of code, runs very slowly and needs me to do lots of string searching, processing the raw html text of the results page. It is also unreliable as sometimes the results page doesn't always load fully before I start parsing the text. Using Indy promises to be much faster and neater. Commented Feb 10, 2014 at 22:40

1 Answer 1

7

You are posting your data to this URL, which is wrong:

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

That is why you are getting the initial page back again - that is what you are actually asking for!

You need to post to this URL instead:

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

And BTW, you do not need the TStringStream at all, Post() can return the string data for you:

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant, thank you. That got me the correct text - and fast! Now, is there an easy/fast way to extract the table data I want other than using lots of pos(s, SomeStringInTheHtml) ie processsing the returned html as if its one big string?
If the HTML is actually XHTML then you can use an XML parser. Otherwise, there are third-party HTML parsers available. Or just parse the HTML yourself, there is nothing wrong with that.
No I guess you are right. After all, the HTML parsers must essentially just be doing a string search. Rolling my own parser could even give me more control. Answer accepted, thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.