Question

parse html and submit form

Asked by: 97WideGlide

I am trying to use libcurl to access remote web pages via a bank of anonymous web proxies.  I can go to a particular web proxy URL (for example http://www.antifilters.info/) and retrieve their page just fine.  The problem is I now need to parse the returned HTML, find out which "input-type=submit" form to use and fill all associated form fields before submitting.  I'm trying to use libxml but cannot figure out how to do it.  Can anyone give me a clue?

Thanks,
Curt

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-07-13 at 21:31:14ID24567652
Tags

C C++ libcurl libxml xml

Topics

C Programming Language

,

Extensible HTML (XHTML)

,

Hypertext Markup Language (HTML)

Participating Experts
2
Points
500
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. par file
    Waht is a file with .par used for and what does this extension mean? Can I list the parameters for import in a .par file and use this file in imp, like imp file=filename.par Please suggest.
  2. par io err laserjet 4 plus
    Every time I start my computer my old laserjet 4 plus comes up with an error message on the control panel "err par io" the work around is to cycle power on the printer after the computer is loaded. This problem started when I got a new computer. I have tried chan...
  3. PAR Protocol
    How has the PAR Protocol been extended for use in the Internet?
  4. Converting from PERL to Executable using PAR
    I have a PERL script that begins with the following: use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; When I convert my PERL script to an executable using pp -o file.exe file.pl, there is no problem. But, when I run file.exe I receive the following pop-...
  5. What does the /par in a .bat file mean?
    I am learning to do batch files, and came across this /par in my .bat file. I dont know really how it got there, as I was just manually creating the batch. I did just install easy batch creator, but I didnt use that prog. Anyway here is the code that I have in my batch, but ...
  6. Unable to implement the Perl Packager utility (pp) which c…
    Installed ActivePerl 5.8.8.817 on Windows 2000 Server with the following modules: Parse::Binary 0.10 Win32::Exe 0.09 Module::ScanDeps 0.62 PAR::Dist 0.16 Getopt::ArgvFile 1.07 Executed the perl Makefile.pl command for PAR 0.951 and the following is returned: C:\Perl\mod\PAR...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: evilrixPosted on 2009-07-13 at 23:45:24ID: 24846781

Try El Kabong, it's a very simple (and very forgiving) Sax style HTML parser.

"El-Kabong is a high-speed, forgiving, sax-style HTML parser. Its aim is to provide consumers with a very fast, clean, lightweight library which parses HTML quickly, while forgiving syntactically incorrect tags."

http://sourceforge.net/projects/ekhtml/

 

by: Infinity08Posted on 2009-07-14 at 00:50:19ID: 24847053

What I generally recommend is to run the HTML through tidy (http://tidy.sourceforge.net/) to clean it up and generate proper XHTML, and then use an XML parser (like libxml : http://xmlsoft.org/) to parse it.

The reason is that a lot of the HTML out there on the internet is full of mistakes, so even a specialized parser can easily "misinterpret" it. Tidy is specialized in cleaning up such HTML.

Of course, you can see if a specialized parser like the one evilrix suggested works for you - if so it would be a bit easier to implement ... I've never tried any, so I can't comment on that :)

 

by: 97WideGlidePosted on 2009-07-14 at 07:03:39ID: 24849585

If we assume that I've got clean XHTML how would I go about forming the data for a proper SUBMIT via libcurl ?

 

by: Infinity08Posted on 2009-07-15 at 01:44:41ID: 24857382

Here is an example of how to perform an HTTP POST using libcurl :

        http://curl.haxx.se/lxr/source/docs/examples/postit2.c

You'll have to know what the form looks like (ie. what fields it has etc.). And since you have the XHTML page that contains the form, you can simply extract the necessary information from it using an XML parser (or HTML parser).

 

by: 97WideGlidePosted on 2009-07-15 at 12:41:49ID: 24863287

HHMM, maybe I"m asking a question that is too involved.  Sorry if I am.  But it is the details regarding how to take the XML after it is tidied up and creating a valid libcurl POST that I am having trouble with.

I'm attaching sample XML code between the <form>...</form> marks.  This part I want to use libcurl to POST.

Thanks,
Curt
 

<form method="post" action="/index.php">
<table width="570" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td>
<div id="address">
<div align="left"><input id="address_box" name="q" type="text"
class="bar" onfocus="this.select()" value="http://www." /></div>
</div>
</td>
<td><input id="button" type="submit" value="" /></td>
</tr>
</table>
<script type="text/javascript">
//<![CDATA[
<!--
google_ad_client = "pub-9576634561657687";
google_ad_width = 468;
google_ad_height = 15;
google_ad_format = "468x15_0ads_al";
google_ad_channel = "";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "ea4b0c";
google_color_text = "666666";
google_color_url = "000000";
//-->
//]]>
</script> <script type="text/javascript" src=
"http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
<label><input type="checkbox" name="hl[remove_scripts]" checked=
"checked" /> Disable JavaScript</label> <label><input type=
"checkbox" name="hl[accept_cookies]" checked="checked" /> Allow
Cookies</label> <label><input type="checkbox" name=
"hl[show_images]" checked="checked" /> Show Images</label>
<label><input type="checkbox" name="hl[base64_encode]" checked=
"checked" /> Use Base64</label> <label><input type="checkbox" name=
"hl[strip_meta]" checked="checked" /> Strip Meta</label>
<label><input type="checkbox" name="hl[include_form]" checked=
"checked" /> Include URL Form</label></form>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window

 

by: Infinity08Posted on 2009-07-16 at 01:48:42ID: 24867506

>> But it is the details regarding how to take the XML after it is tidied up and creating a valid libcurl POST that I am having trouble with.

You need to have a few bits of information :

(a) the submit URI, which can be found in the action attribute of the form tag. If it's a relative path, make sure to turn it into a complete URI.

(b) for each input field (input tag in the HTML), the type, name and value. Some of the values will be pre-defined (like the submit button in your example), some will have to be filled in by you.

All this information can be extracted from the HTML. The tricky bit will be to make your code understand it, and handle it correctly. I don't know what you're trying to do, but it seems you want to get this to work for any site ... which means you'll have to put in some intelligence in your code to understand what each of the fields mean.

Maybe if you could describe in a bit more detail what exactly you are trying to do, ...

 

by: 97WideGlidePosted on 2009-07-16 at 04:44:08ID: 24868465

I'll try to provide more detail regarding exactly what I want to do.

If you go to: http://www.publicproxyservers.com/proxy/list_last_test1.html you will see a table of anonymous web proxies.   In a nutshell, under program control, I want to be able to go to each of those proxies in turn and then (through them)  go anonymously to a different site of my choosing.  The steps I have coded already are:

1-retrieve web page from link above and extract list of URLs of proxies
2-go to the first proxy in the list and retrieve its Web page.

So now, I need to parse the HTML retrieved from the Web Proxy in order to create a valid libcurl POST from the <form method="post"...>...</form> areas of the HTML.

I appreciate you taking an interest in my problem.


 

by: Infinity08Posted on 2009-07-16 at 06:05:30ID: 24868578

So, you basically want to extract form information from a web page without ptior knowledge of that page. All that is known, is that it's supposed to contain a form that allows to enter a URI, and then submit it to get the result. Right ?

In other words, your code will have to be quite intelligent to get around all pitfalls and gotchas involved in this, for example :

(a) some pages might contain more than one form (maybe a search form, or a comment form, or ...) - how will you know which one is the one you need ?

(b) some of these forms might require you to fill in more than just the URI, and might have a different way of submitting. How will you know how to use the form ?

(c) some of these forms might have multiple fields - how will you find the one that is supposed to contain the URI ?

(d) on some pages, JavaScript or other such things might be involved, which complicates the task of submitting automatically.

Just to mention a few.

This is by no means a simple task. For a human user of the website, things are straightforward, since he can see the web page, and understands what he's supposed to do (given all the meta information, and his experience with using a browser). But writing code to perform the same actions is more difficult, since you have to basically make it understand the website, and what's supposed to happen.

An easier approach is probably to gather a list of such web proxies, and all the necessary information, store that in a resource file somewhere (in a format of your choice), and then your code can simply use that resource file to do whatever it needs to do. That way, you perform the actions a human is good at yourself, and leave the rest up to your code.

 

by: 97WideGlidePosted on 2009-07-16 at 06:53:33ID: 24869031

a) If the page has > 1 form tag I can simply submit the first one found and see if I get the target page back.  If not, try the following one until I get the expected result.

b) A web browser knows how to parse this information and it is pretty well defined at www.w3.org so I am not too concerned about that.  In fact I am just about to break down and just write my own parser.

c) I think this should be pretty easy to guess.  i.e. type="text" (value="" or "http://" or ...)

d) not sure about javascript but I just don't think it can be that difficult.

Am I wrong?

 

by: Infinity08Posted on 2009-07-16 at 07:35:19ID: 24869528

>> b) A web browser knows how to parse this information and it is pretty well defined at www.w3.org so I am not too concerned about that.  In fact I am just about to break down and just write my own parser.

A web browser just displays whatever the HTML tells it to. You as the user actually interpret the results, fill in the appropriate fields, and click the appropriate button. That's something that is not part of the W3C standard.


>> c) I think this should be pretty easy to guess.  i.e. type="text" (value="" or "http://" or ...)

What if there's multiple text fields ?


>> Am I wrong?

I guess you'll find out ;) I'm not saying it's impossible ... I'm saying it's not trivial, and might involve quite a bit of work to get it working right. If that doesn't scare you off, then go for it ! :)

 

by: 97WideGlidePosted on 2009-07-16 at 07:50:41ID: 24869696

The work doesn't scare me but getting back to the original question, does anyone know how to parse the <form> tag and create a valid POST via libcurl ?

 

by: Infinity08Posted on 2009-07-16 at 08:07:33ID: 24869895

>> does anyone know how to parse the <form> tag and create a valid POST via libcurl ?

I thought that was what I was answering ;)

But I guess you're looking more for the technical side of things. If so, then check back in the first few posts, where either a HTML parser or an XML parser was suggested. If you run the HTML through such a parser, you'll get a tree as output that represents the format of the HTML. You can then iterate over that tree to get the elements you want. For specific examples, you can always refer to the documentation for the specific library you intend to use.

Once you have the data you need, you can POST it using libcurl. For that, see the sample code I mentioned earlier.

 

by: 97WideGlidePosted on 2009-07-16 at 08:27:30ID: 24870121

Damn, I've looked all over the web for hours and can't find any sample code.  Below is what I have so far.  I can get the name of each element but I can't figure out how to get the strings associated with that content.

  
// Here I have already read the remote file into memory and tidied it up with libtidy.
//      The retrieved HTML is in output.bp
 
  TidyBuffer output = {0};
 
  xmlDocPtr doc; /* the resulting document tree */
 
  doc = xmlReadMemory((const char *)output.bp, output.size, "noname.xml", NULL, 0);
  if (doc == NULL) {
      fprintf(stderr, "Failed to parse document\n");
  }
 
  // now traverse the tree to find POST
  print_element_names(xmlDocGetRootElement(doc));
 
===========================================================
 
static void
print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;
 
    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE) {
      		printf("%s\n", cur_node->name;
        }
        print_element_names(cur_node->children);
    }
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

Select allOpen in new window

 

by: Infinity08Posted on 2009-07-16 at 08:50:26ID: 24870400

>> I can get the name of each element but I can't figure out how to get the strings associated with that content.

What do you mean by "strings" ? Which strings ?

You probably want to start by finding an element with name "form", and then the child elements with name "input". You can get attributes of an element using xmlGetProp, like in :

        http://xmlsoft.org/tutorial/ar01s08.html

Note that the rest of that tutorial might also be interesting to you :

        http://xmlsoft.org/tutorial/index.html

 

by: 97WideGlidePosted on 2009-07-16 at 09:42:06ID: 24871004

That looks to be exactly what I am looking for.  I'm going to try coding it up in a few hours and I'll report back.   Thank you.

Man, don't know why I couldn't find it.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...