{"id":194509,"date":"2024-11-13T07:15:22","date_gmt":"2024-11-12T23:15:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194509\/"},"modified":"2024-11-13T07:15:23","modified_gmt":"2024-11-12T23:15:23","slug":"%e7%94%a8c%e7%b7%a8%e7%a8%8b%e5%be%9e%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%8f%90%e5%8f%96%e5%85%a7%e5%ae%b9-c-%e7%b7%a8%e7%a8%8b%e6%8f%90%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194509\/","title":{"rendered":"\u7528C\u7de8\u7a0b\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9 (c \u7de8\u7a0b\u63d0\u53d6\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u5167\u5bb9)"},"content":{"rendered":"<h1 id=\"%e7%94%a8c%e7%b7%a8%e7%a8%8b%e5%be%9e%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%8f%90%e5%8f%96%e5%85%a7%e5%ae%b9-OrBYVfBjLY\">\u7528C\u7de8\u7a0b\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u5b57\u5316\u7684\u6642\u4ee3\uff0c\u6578\u64da\u7684\u63d0\u53d6\u548c\u8655\u7406\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u7279\u5225\u662f\u5728\u7db2\u9801\u6578\u64da\u5eab\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u63d0\u53d6\u6240\u9700\u7684\u5167\u5bb9\u6210\u70ba\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u79d1\u5b78\u5bb6\u7684\u95dc\u6ce8\u7126\u9ede\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u7de8\u7a0b\u8a9e\u8a00\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87c%e8%aa%9e%e8%a8%80%ef%bc%9f-OrBYVfBjLY\">\u70ba\u4ec0\u9ebc\u9078\u64c7C\u8a9e\u8a00\uff1f<\/h2>\n<p>C\u8a9e\u8a00\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7cfb\u7d71\u7de8\u7a0b\u548c\u5d4c\u5165\u5f0f\u7cfb\u7d71\u3002\u5176\u512a\u52e2\u5728\u65bc\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u512a\u8d8a\uff1a<\/strong> C\u8a9e\u8a00\u7684\u57f7\u884c\u901f\u5ea6\u5feb\uff0c\u9069\u5408\u8655\u7406\u5927\u91cf\u6578\u64da\u3002<\/li>\n<li><strong>\u63a7\u5236\u529b\u5f37\uff1a<\/strong> \u958b\u767c\u8005\u53ef\u4ee5\u5c0d\u5167\u5b58\u7ba1\u7406\u9032\u884c\u7cbe\u7d30\u63a7\u5236\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u63d0\u53d6\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u6027\uff1a<\/strong> C\u8a9e\u8a00\u53ef\u4ee5\u5728\u591a\u7a2e\u64cd\u4f5c\u7cfb\u7d71\u4e0a\u904b\u884c\uff0c\u589e\u52a0\u4e86\u5176\u9748\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%8f%90%e5%8f%96%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-OrBYVfBjLY\">\u63d0\u53d6\u7db2\u9801\u6578\u64da\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9\u7684\u904e\u7a0b\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li><strong>\u767c\u9001HTTP\u8acb\u6c42\uff1a<\/strong> \u4f7f\u7528C\u8a9e\u8a00\u7684\u5eab\u4f86\u767c\u9001\u8acb\u6c42\uff0c\u7372\u53d6\u7db2\u9801\u5167\u5bb9\u3002<\/li>\n<li><strong>\u89e3\u6790HTML\uff1a<\/strong> \u5c07\u7372\u53d6\u7684HTML\u5167\u5bb9\u9032\u884c\u89e3\u6790\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u5b58\u5132\uff1a<\/strong> \u5c07\u63d0\u53d6\u7684\u6578\u64da\u5b58\u5132\u5230\u672c\u5730\u6578\u64da\u5eab\u6216\u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ol>\n<h2 id=\"%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e7%99%bc%e9%80%81http%e8%ab%8b%e6%b1%82-OrBYVfBjLY\">\u4f7f\u7528C\u8a9e\u8a00\u767c\u9001HTTP\u8acb\u6c42<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528libcurl\u5eab\u4f86\u767c\u9001HTTP\u8acb\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;curl\/curl.h&gt;\n\nsize_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {\n    ((std::string*)userp)-&gt;append((char*)contents, size * nmemb);\n    return size * nmemb;\n}\n\nint main(void) {\n    CURL *curl;\n    CURLcode res;\n    std::string readBuffer;\n\n    curl = curl_easy_init();\n    if(curl) {\n        curl_easy_setopt(curl, CURLOPT_URL, \"http:\/\/example.com\");\n        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);\n        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;readBuffer);\n        res = curl_easy_perform(curl);\n        curl_easy_cleanup(curl);\n    }\n    printf(\"%sn\", readBuffer.c_str());\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528libcurl\u5eab\u4f86\u767c\u9001GET\u8acb\u6c42\uff0c\u4e26\u5c07\u8fd4\u56de\u7684\u5167\u5bb9\u5b58\u5132\u5728\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<h2 id=\"%e8%a7%a3%e6%9e%90html%e5%85%a7%e5%ae%b9-OrBYVfBjLY\">\u89e3\u6790HTML\u5167\u5bb9<\/h2>\n<p>\u7372\u53d6HTML\u5167\u5bb9\u5f8c\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u89e3\u6790\u9019\u4e9b\u5167\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528HTML\u89e3\u6790\u5eab\uff0c\u5982Gumbo\u6216libxml2\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Gumbo\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n#include &lt;gumbo.h&gt;\n\nvoid search_for_links(GumboNode* node) {\n    if (node-&gt;type == GUMBO_NODE_ELEMENT &amp;&amp; node-&gt;v.element.tag == GUMBO_TAG_A) {\n        GumboAttribute* href = gumbo_get_attribute(&amp;node-&gt;v.element.attributes, \"href\");\n        if (href) {\n            printf(\"Link: %sn\", href-&gt;value);\n        }\n    }\n    for (int i = 0; i &lt; node-&gt;v.element.children.length; ++i) {\n        search_for_links(static_cast&lt;GumboNode*&gt;(node-&gt;v.element.children.data[i]));\n    }\n}\n\nint main() {\n    GumboOutput* output = gumbo_parse(readBuffer.c_str());\n    search_for_links(output-&gt;root);\n    gumbo_destroy_output(&amp;kGumboDefaultOptions, output);\n    return 0;\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u904d\u6b77HTML\u6a39\uff0c\u4e26\u63d0\u53d6\u6240\u6709\u7684\u93c8\u63a5\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2-OrBYVfBjLY\">\u6578\u64da\u5b58\u5132<\/h2>\n<p>\u63d0\u53d6\u5230\u7684\u6578\u64da\u53ef\u4ee5\u5b58\u5132\u5230\u672c\u5730\u6587\u4ef6\u6216\u6578\u64da\u5eab\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528C\u8a9e\u8a00\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6578\u4f86\u5be6\u73fe\u9019\u4e00\u9ede\uff1a<\/p>\n<pre><code>\nFILE *file = fopen(\"output.txt\", \"w\");\nif (file) {\n    fprintf(file, \"%sn\", extracted_data.c_str());\n    fclose(file);\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OrBYVfBjLY\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528C\u7de8\u7a0b\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9\u662f\u4e00\u500b\u6d89\u53ca\u591a\u500b\u6b65\u9a5f\u7684\u904e\u7a0b\uff0c\u5305\u62ec\u767c\u9001HTTP\u8acb\u6c42\u3001\u89e3\u6790HTML\u548c\u6578\u64da\u5b58\u5132\u3002\u901a\u904e\u4f7f\u7528libcurl\u548cGumbo\u7b49\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u9019\u4e00\u4efb\u52d9\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u7684\u61c9\u7528\uff0cC\u8a9e\u8a00\u7121\u7591\u662f\u4e00\u500b\u7406\u60f3\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u63d0\u53d6\u9805\u76ee\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u60a8\u90fd\u53ef\u4ee5\u5728\u6211\u5011\u7684\u7db2\u7ad9\u4e0a\u627e\u5230\u5408\u9069\u7684\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528C\u7de8\u7a0b\u5f9e\u7db2\u9801\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u5167\u5bb9\uff0c\u8f15\u9b06\u7372\u53d6\u6240\u9700\u6578\u64da\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u5be6\u73fe\u6578\u64da\u81ea\u52d5\u5316\u8655\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-194509","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=194509"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194509\/revisions"}],"predecessor-version":[{"id":194510,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194509\/revisions\/194510"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}