C language calls the curl library to capture web images and curl to capture

Source: Internet
Author: User

C language calls the curl library to capture web images and curl to capture
The idea is to capture the webpage source code with curl and then search for the image URL with the keyword. # Include <stdio. h> # include <stdlib. h> # include <string. h> # include <curl/curl. h> void get_key_from_str (char * origin, char * str1, char * str2, char * key); int main (int argc, char ** argv) {CURL * curl; FILE * fp = NULL, * fp_read = NULL; int file_size = 0; char * tmp = NULL; int flag = 0; char key [1024] = {0 }; char str1 [128] = {0}; char str2 [128] = {0}; if (fp = fopen ("test.txt", "w") = NULL) {return 1 ;} Curl = curl_easy_init (); if (curl) {flag = 1; curl_easy_setopt (curl, CURLOPT_URL, argv [1]); // curl sets the URL curl_easy_setopt (curl, expires, fp); // Save the webpage source code to a file. It seems that only files can be saved, and the string curl_easy_perform (curl) cannot be saved; // start to capture curl_easy_cleanup (curl); fclose (fp ); fp_read = fopen ("test.txt", "rb"); fseek (fp_read, 0, SEEK_END); // move the pointer to the end of the text file_size = ftell (fp_read ); // calculate the text size fseek (fp_read, 0, SEEK_SET); // move the pointer To the front tmp = (char *) malloc (file_size * sizeof (char); // malloc a piece of memory printf ("file_size: % d \ n", file_size ); fread (tmp, file_size, sizeof (char), fp_read); // read the file fclose (fp_read); snprintf (str1, sizeof (str1 ), "img src = \" "); snprintf (str2, sizeof (str2)," \ ""); get_key_from_str (tmp, str1, str2, key ); // obtain the URL printf ("key: % s \ n", key); free (tmp);} if (! Flag) {fclose (fp);} return 0;} // capture the keyword void get_key_from_str (char * origin, char * str1, char * str2, char * key) {char * p = strstr (origin, str1); char * q = strstr (p + strlen (str1), str2 ); int len = q-p-strlen (str1); snprintf (key, len + 1, "% s", p + strlen (str1 )); key [len + 1] = '\ 0';} gcc-g-Wall main. c-o test-lcurl. /test url

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.