开发者

Interact with websites C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_Python百科

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

How do I interact with websites in C++?

For example, a website has a dropbox, text area and a button, and I want my program to fill text inside the text area, choose an option from the dropbox, and make the button fire its event("clicking" it).

How can I achieve something like that?

thanks!


First you have to understand that on the server there is no text box or button.

These are constructs that are built by the browser to display to you.

The browser will then take user input into the text box and interprets the clicks on the button. What happens (usually) when the button is clicked is that the browser sends an HTML "POST" request to the server. The browser builds the post request based on what the user has done in the UI.

Example:

Server Sends to browser:

<html>
    <head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title>TestDoc</title></head>
    <body>
        <form action="http://website.com/form.html" method="post">
        <div>
            <textarea name="userinfo" rows="2" cols="30">Some Text</textarea>
            <input type="submit">
        </div>
        </form>
    </body>
</html>

Your browser interprets this and displays a text box and submit button. When you press the submit button the browser builds an HTTP post command that is sent back to the browser. It connects to the website at http://website.com/form.html (see form tag in code snippet above) and sends the content of the text area (tagged with the value userinfo).

You can manually do the same.
But you need to understand what values the website is expecting and build the appropriate command based on what the website is expecting. To do this, the easiest way is to use libCurl. The documentation for this package explains in detail how to build a post request.

Here is a post example:


Take a look al libcurl. It's a C library, but you could definitely use it from C++ to achieve what you want.

If you need just to do some casual WebSite interaction I would suggest you to take a look at languages that are more suitable (and easier to use) than C++ for that task. Python (with Mechanize library), Ruby, PHP, Perl...

Even Java and C# have native libraries to deal with stuff like this.


Okay, it really depends on a few things. You may be overcomplicating your problem by thinking that you need to get the program to fill in the form as though it was a human. Instead, why don't you skip that step?

When you submit a form, your browser sends a HTTP POST (usually) request across the Internet with your form details in the header. If you know what the form is going to be beforehand, you can simply get the program to send POST requests to the server as though somebody had submitted a form.

If you don't know what the form will be, then you need to send a GET request to the server to retrieve the page and then somehow analyse the page to extract the information needed to fill the form details (that's your problem) and then send the POST request with the details as above.

This can all be done with the libcurl library. It allows you to send HTTP requests through a simple interface.

Now, if your problem actually requires you to make a program that acts as a human and really manipulates the browser to fill in the form, then you need to learn about simulating key presses and mouse clicks, which is likely going to be platform dependent.


If libcurl is not enough for your needs you might want to take a look at Qt, specifically the QtWebKit module. It basically incorporates a complete browser engine which also Google Chrome uses. Using that engine you can even execute your own JavaScript code in the context of the Website and simulate for instance a login .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜