{"id":59675,"date":"2024-10-12T23:54:16","date_gmt":"2024-10-12T15:54:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59675\/"},"modified":"2024-10-12T23:54:16","modified_gmt":"2024-10-12T15:54:16","slug":"c%e8%aa%9e%e8%a8%80%e7%b7%a8%e7%a8%8b%e5%af%a6%e7%8f%be%e9%9d%9c%e6%85%8b%e7%b6%b2%e7%ab%99%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96-%e4%bd%bf%e7%94%a8c%e7%88%ac%e5%8f%96%e9%9d%9c%e6%85%8b%e7%b6%b2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59675\/","title":{"rendered":"C\u8a9e\u8a00\u7de8\u7a0b\u5be6\u73fe\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6 (\u4f7f\u7528c\u722c\u53d6\u975c\u614b\u7db2\u7ad9\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"c%e8%aa%9e%e8%a8%80%e7%b7%a8%e7%a8%8b%e5%af%a6%e7%8f%be%e9%9d%9c%e6%85%8b%e7%b6%b2%e7%ab%99%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96-BAARCYVOrh\">C\u8a9e\u8a00\u7de8\u7a0b\u5be6\u73fe\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u722c\u53d6\u6280\u8853\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u7121\u8ad6\u662f\u70ba\u4e86\u5e02\u5834\u7814\u7a76\u3001\u7af6\u722d\u5206\u6790\uff0c\u9084\u662f\u6578\u64da\u6536\u96c6\uff0c\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6\u90fd\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u4f86\u5be6\u73fe\u975c\u614b\u7db2\u7ad9\u7684\u6578\u64da\u722c\u53d6\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e9%9d%9c%e6%85%8b%e7%b6%b2%e7%ab%99%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-BAARCYVOrh\">\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u975c\u614b\u7db2\u7ad9\u662f\u6307\u5176\u5167\u5bb9\u4e0d\u6703\u96a8\u7528\u6236\u7684\u8acb\u6c42\u800c\u6539\u8b8a\u7684\u7db2\u7ad9\u3002\u9019\u985e\u7db2\u7ad9\u901a\u5e38\u7531HTML\u6587\u4ef6\u7d44\u6210\uff0c\u4e26\u4e14\u4e0d\u9700\u8981\u5f8c\u7aef\u6578\u64da\u5eab\u652f\u6301\u3002\u6578\u64da\u722c\u53d6\u7684\u904e\u7a0b\u4e3b\u8981\u5305\u62ec\u767c\u9001HTTP\u8acb\u6c42\u3001\u63a5\u6536\u97ff\u61c9\u3001\u89e3\u6790HTML\u5167\u5bb9\u4ee5\u53ca\u63d0\u53d6\u6240\u9700\u6578\u64da\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96%e7%9a%84%e6%ad%a5%e9%a9%9f-BAARCYVOrh\">\u4f7f\u7528C\u8a9e\u8a00\u9032\u884c\u6578\u64da\u722c\u53d6\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-BAARCYVOrh\">1. \u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u958b\u767c\u74b0\u5883\u4e2d\u5df2\u5b89\u88ddC\u8a9e\u8a00\u7de8\u8b6f\u5668\uff0c\u5982GCC\u3002\u6b64\u5916\uff0c\u60a8\u9084\u9700\u8981\u5b89\u88ddlibcurl\u5eab\uff0c\u9019\u662f\u4e00\u500b\u7528\u65bc\u8655\u7406URL\u7684\u958b\u6e90\u5eab\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u767c\u9001HTTP\u8acb\u6c42\u3002<\/p>\n<h3 id=\"2-%e5%ae%89%e8%a3%9dlibcurl-BAARCYVOrh\">2. \u5b89\u88ddlibcurl<\/h3>\n<pre><code>sudo apt-get install libcurl4-openssl-dev<\/code><\/pre>\n<h3 id=\"3-%e7%99%bc%e9%80%81http%e8%ab%8b%e6%b1%82-BAARCYVOrh\">3. \u767c\u9001HTTP\u8acb\u6c42<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684C\u8a9e\u8a00\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528libcurl\u767c\u9001HTTP GET\u8acb\u6c42\uff1a<\/p>\n<pre><code>#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\n    printf(\"%sn\", readBuffer.c_str());\n    return 0;\n}<\/code><\/pre>\n<h3 id=\"4-%e8%a7%a3%e6%9e%90html%e5%85%a7%e5%ae%b9-BAARCYVOrh\">4. \u89e3\u6790HTML\u5167\u5bb9<\/h3>\n<p>\u7372\u53d6\u5230HTML\u5167\u5bb9\u5f8c\uff0c\u60a8\u9700\u8981\u89e3\u6790\u9019\u4e9b\u6578\u64da\u3002C\u8a9e\u8a00\u672c\u8eab\u4e26\u4e0d\u63d0\u4f9b\u5167\u5efa\u7684HTML\u89e3\u6790\u529f\u80fd\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\uff0c\u5982libxml2\u6216Gumbo Parser\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528libxml2\u89e3\u6790HTML\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>#include &lt;libxml\/HTMLparser.h&gt;\n\nvoid parseHTML(const char *html) {\n    htmlDocPtr doc = htmlReadMemory(html, strlen(html), NULL, NULL, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);\n    xmlNode *root_element = xmlDocGetRootElement(doc);\n    \/\/ \u9032\u4e00\u6b65\u89e3\u6790...\n    xmlFreeDoc(doc);\n}\n\nint main(void) {\n    \/\/ \u5047\u8a2dreadBuffer\u5305\u542b\u4e86HTML\u5167\u5bb9\n    parseHTML(readBuffer.c_str());\n    return 0;\n}<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-BAARCYVOrh\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u9075\u5faa\u7db2\u7ad9\u7684robots.txt\u898f\u7bc4\uff0c\u78ba\u4fdd\u60a8\u7684\u722c\u53d6\u884c\u70ba\u4e0d\u9055\u53cd\u7db2\u7ad9\u7684\u4f7f\u7528\u689d\u6b3e\u3002<\/li>\n<li>\u907f\u514d\u904e\u65bc\u983b\u7e41\u7684\u8acb\u6c42\uff0c\u4ee5\u514d\u5c0d\u76ee\u6a19\u7db2\u7ad9\u9020\u6210\u8ca0\u64d4\u3002<\/li>\n<li>\u8003\u616e\u4f7f\u7528\u4ee3\u7406\u4f3a\u670d\u5668\u4f86\u96b1\u85cf\u60a8\u7684IP\u5730\u5740\uff0c\u7279\u5225\u662f\u5728\u5927\u898f\u6a21\u722c\u53d6\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-BAARCYVOrh\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528C\u8a9e\u8a00\u9032\u884c\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6\u662f\u4e00\u500b\u6709\u8da3\u4e14\u5177\u6311\u6230\u6027\u7684\u4efb\u52d9\u3002\u901a\u904e\u4f7f\u7528libcurl\u548cHTML\u89e3\u6790\u5eab\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u7372\u53d6\u548c\u8655\u7406\u7db2\u7ad9\u6578\u64da\u3002\u9019\u4e9b\u6280\u8853\u4e0d\u50c5\u9069\u7528\u65bc\u500b\u4eba\u9805\u76ee\uff0c\u4e5f\u53ef\u4ee5\u61c9\u7528\u65bc\u5546\u696d\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684\u722c\u87f2\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u7121\u8ad6\u662f\u6578\u64da\u722c\u53d6\u9084\u662f\u5176\u4ed6\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684<a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\u90fd\u80fd\u63d0\u9ad8\u60a8\u7684\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528C\u8a9e\u8a00\u7de8\u7a0b\u5be6\u73fe\u975c\u614b\u7db2\u7ad9\u6578\u64da\u722c\u53d6\uff0c\u8f15\u9b06\u63d0\u53d6\u7db2\u7ad9\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-59675","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59675","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=59675"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59675\/revisions"}],"predecessor-version":[{"id":59676,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59675\/revisions\/59676"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}