利用cURL来获取网页信息-Using cURL to get webpage content

本文介绍了一个使用C++和libcurl库下载指定URL网页内容的示例程序。该程序展示了如何设置curl选项来获取网页内容,并通过字符串缓冲区保存结果。此外,还实现了将下载内容按行拆分并进一步解析的功能。
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>

extern "C" {
#include <stdio.h>
//#include <curl/easy.h>
#include <curl/curl.h>
}

using namespace std;

int writer(char* data, size_t size, size_t nmemb, string *buffer) {
        fprintf(stderr, "Hello, I am a function pointer\n");

        int result = 0;
        if (buffer != NULL) {
                buffer->append(data, size * nmemb);
                result = size * nmemb;
        }

        return result;
}

int main(int argc, char** args) {
        CURL* curl; // That is the connection, see : curl_easy_init
        CURLcode res; // Not needed, see : curl_easy_cleanup

        string buffer; // see: CURLOPT_WRITEDATA

        curl = curl_easy_init(); // Initilize web query

        if (curl) {
                // set options for web query
                curl_easy_setopt(curl, CURLOPT_URL, args[1]);
                curl_easy_setopt(curl, CURLOPT_HEADER, 0); // No we don't need the header of the web content, Set to 0 and 
                                                                                                        // curl ignores the first line
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);//Don't follow anything else than the particular url requested
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); // Function Pointer 'writer' manages the required buffer size
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); // Data Pointer &buffer stores downloaded web content
        } else {
                fprintf(stderr, "Error 1\n");
                return 0;
        }

        res = curl_easy_perform(curl);
        //string s = (res);
        cout << res << endl;
        curl_easy_cleanup(curl);

        std::istringstream iss(buffer);
        string line, item;

        int linenum = 0;

        while (getline(iss, line)) {
                linenum ++;
                cout << "\nline #" << linenum << " : " << endl;
                std::istringstream linestream(line);

                int itemnum = 0;

                while (getline(linestream, item, ',')) {
                        itemnum ++;
                        cout << "Item # " << itemnum << " : " << item << endl;
                }
        }

        return 0;
}

curl是可以的,但是用ytb dl不行 C:\Users\kunlin.yang>curl -I "https://ak.picdn.net/shutterstock/videos/1060117940/preview/stock-footage-portrait-of-disappointed-young-indian-businesswoman-doing-thumbs-down.mp4" HTTP/1.0 200 Connection Established HTTP/1.1 200 OK Age: 2992 Via: 1.1 a8d4a5a03f7aa29b93edc3d18aee5d2a.cloudfront.net (CloudFront) Date: Wed, 10 Sep 2025 02:47:15 GMT ETag: "a4dc8940bfe3e19445ee08776307c2b7" Vary: Origin Server: AmazonS3 Alt-Svc: h3=":443"; ma=86400 X-Cache: Hit from cloudfront Connection: Keep-Alive X-Amz-Cf-Id: FoeMeHCLiDpI5a4SeC4ZhjGozYqhjnNGJ8NfGxeF2bHD7LAupLJUOQ== Content-Type: video/mp4 X-Amz-Cf-Pop: LAX54-P10 Accept-Ranges: bytes cache-control: max-age=2592000 Last-Modified: Tue, 06 Oct 2020 00:06:11 GMT Content-Length: 381440 x-amz-version-id: fvc6IfvOC4bY5feZ8.a7RkwQf6kivEX. x-amz-storage-class: INTELLIGENT_TIERING x-amz-replication-status: COMPLETED C:\Users\kunlin.yang>yt-dlp -f mp4 -o "output.mp4" --proxy "http://109.105.230.22:9090" "https://ak.picdn.net/shutterstock/videos/1060117940/preview/stock-footage-portrait-of-disappointed-young-indian-businesswoman-doing-thumbs-down.mp4" WARNING: "-f mp4" selects the best pre-merged mp4 format which is often not what's intended. Pre-merged mp4 formats are not available from all sites, or may only be available in lower quality. To prioritize the best h264 video and aac audio in an mp4 container, use "-t mp4" instead. If you know what you are doing and want a pre-merged mp4 format, use "-f b[ext=mp4]" instead to suppress this warning [generic] Extracting URL: https://ak.picdn.net/shutterstock/videos/1060117940/preview/stock-footage-portrait-of-disappointe...oing-thumbs-down.mp4 [generic] stock-footage-portrait-of-disappointed-young-indian-businesswoman-doing-thumbs-down: Downloading webpage ERROR: [generic] Unable to download webpage: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1028) (caused by CertificateVerifyError('[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1028)')); please report this issue on https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the appropriate issue template. Confirm you are on the latest version using yt-dlp -U
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值