{"id":9946,"date":"2025-06-27T10:21:11","date_gmt":"2025-06-27T02:21:11","guid":{"rendered":"https:\/\/www.cloudbypass.com\/tutorial\/?p=9946"},"modified":"2025-06-27T10:21:11","modified_gmt":"2025-06-27T02:21:11","slug":"%e6%8b%bf%e5%88%b0html%e4%b9%8b%e5%90%8e-%e7%94%a8python%e9%ab%98%e6%95%88%e8%a7%a3%e6%9e%90%e7%a9%bf%e4%ba%91api%e8%bf%94%e5%9b%9e%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.cloudbypass.com\/tutorial\/9946.html","title":{"rendered":"\u62ff\u5230HTML\u4e4b\u540e &#8211; \u7528Python\u9ad8\u6548\u89e3\u6790\u7a7f\u4e91API\u8fd4\u56de\u7684\u6570\u636e"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u606d\u559c\u4f60\uff01\u901a\u8fc7\u4f7f\u7528<strong>\u4e13\u4e1a\u7ea7\u53cd\u53cd\u722c\u89e3\u51b3\u65b9\u6848<\/strong>\u2014\u2014\u7a7f\u4e91API\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u5730\u7ed5\u8fc7\u4e86Cloudflare\u7684\u6240\u6709\u9632\u62a4\uff0c\u62ff\u5230\u4e86\u68a6\u5bd0\u4ee5\u6c42\u7684\u3001\u5e72\u51c0\u7684HTML\u9875\u9762\u6e90\u4ee3\u7801\u3002\u4f46\u8fd9\u53ea\u662f\u4e07\u91cc\u957f\u5f81\u7684\u7b2c\u4e00\u6b65\u3002\u5982\u4f55\u4ece\u8fd9\u5806\u770b\u4f3c\u6742\u4e71\u7684HTML\u6807\u7b7e\u4e2d\uff0c\u9ad8\u6548\u3001\u51c6\u786e\u5730\u63d0\u53d6\u51fa\u4f60\u6240\u9700\u8981\u7684\u6570\u636e\uff0c\u662f\u63a5\u4e0b\u6765\u540c\u6837\u91cd\u8981\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u5c06\u9762\u5411Python\u5f00\u53d1\u8005\uff0c\u5728\u4f60\u6210\u529f\u4f7f\u7528\u7a7f\u4e91API\u83b7\u53d6\u5230HTML\u4e4b\u540e\uff0c\u5bf9\u51e0\u6b3e\u6700\u4e3b\u6d41\u7684Python\u89e3\u6790\u5e93\u8fdb\u884c\u6bd4\u8f83\u548c\u4ecb\u7ecd\uff0c\u52a9\u4f60\u5b8c\u6210\u6570\u636e\u91c7\u96c6\u7684\u201c\u6700\u540e\u4e00\u516c\u91cc\u201d\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.cloudbypass.com\/tutorial\/wp-content\/uploads\/2025\/05\/image-2025-05-07T100740.756.jpg\" alt=\"\" title=\"\"><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e00\u3001\u89e3\u6790\u5e93\u7684\u201c\u4e09\u5de8\u5934\u201d\uff1aLXML, BeautifulSoup, Parsel<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Python\u7684\u4e16\u754c\u91cc\uff0cHTML\/XML\u7684\u89e3\u6790\u4e3b\u8981\u7531\u8fd9\u4e09\u5927\u5bb6\u65cf\u6240\u7edf\u6cbb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. LXML\uff1a\u6027\u80fd\u4e4b\u738b<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6838\u5fc3<\/strong>\uff1a\u57fa\u4e8eC\u8bed\u8a00\u5e93<code>libxml2<\/code>\u548c<code>libxslt<\/code>\u6784\u5efa\uff0c\u56e0\u6b64\u5176\u89e3\u6790\u901f\u5ea6\u662f\u4e09\u8005\u4e2d\u6700\u5feb\u7684\uff0c\u5185\u5b58\u5360\u7528\u4e5f\u76f8\u5bf9\u8f83\u4f4e\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u65b9\u5f0f<\/strong>\uff1a\u4e3b\u8981\u901a\u8fc7XPath\u548cCSS\u9009\u62e9\u5668\u8fdb\u884c\u8282\u70b9\u9009\u62e9\u3002\u5176XPath\u652f\u6301\u975e\u5e38\u5b8c\u6574\u548c\u5f3a\u5927\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u6027\u80fd\u6781\u81f4\uff0c\u89e3\u6790\u901f\u5ea6\u5feb\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u578b\u3001\u590d\u6742\u7684HTML\u6587\u6863\u3002<\/li>\n\n\n\n<li><strong>\u7f3a\u70b9<\/strong>\uff1aAPI\u76f8\u5bf9\u5e95\u5c42\uff0c\u5bb9\u9519\u6027\u7a0d\u5dee\u3002\u5bf9\u4e8e\u4e00\u4e9b\u4e0d\u89c4\u8303\u7684HTML\uff0c\u53ef\u80fd\u4f1a\u89e3\u6790\u5931\u8d25\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. BeautifulSoup (BS4)\uff1a\u6613\u7528\u6027\u4e4b\u738b<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6838\u5fc3<\/strong>\uff1a\u7eafPython\u5b9e\u73b0\uff0c\u4ee5\u5176\u6781\u5176\u53cb\u597d\u548c\u7b26\u5408\u4eba\u7c7b\u76f4\u89c9\u7684API\u800c\u95fb\u540d\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u65b9\u5f0f<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u3001Pythonic\u7684\u8282\u70b9\u904d\u5386\u548c\u641c\u7d22\u65b9\u6cd5\uff08\u5982<code>find()<\/code>\u3001<code>find_all()<\/code>\uff09\uff0c\u540c\u65f6\u4e5f\u652f\u6301CSS\u9009\u62e9\u5668\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u70b9<\/strong>\uff1aAPI\u7b80\u5355\u6613\u7528\uff0c\u5b66\u4e60\u66f2\u7ebf\u5e73\u7f13\u3002\u5bb9\u9519\u6027\u6781\u5f3a\uff0c\u80fd\u5f88\u597d\u5730\u5904\u7406\u5404\u79cd\u4e0d\u89c4\u8303\u3001\u751a\u81f3\u201c\u7834\u788e\u201d\u7684HTML\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u6027\u80fd\u76f8\u5bf9\u8f83\u6162\uff0c\u5bf9\u4e8e\u8d85\u5927\u578b\u6587\u4ef6\u7684\u89e3\u6790\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Parsel\uff1aScrapy\u7684\u201c\u5fa1\u7528\u201d\u89e3\u6790\u5e93<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6838\u5fc3<\/strong>\uff1a\u7531\u8457\u540d\u7684\u722c\u866b\u6846\u67b6Scrapy\u56e2\u961f\u5f00\u53d1\u548c\u7ef4\u62a4\uff0c\u5b83\u5b9e\u9645\u4e0a\u662f\u5728LXML\u7684\u57fa\u7840\u4e0a\uff0c\u5c01\u88c5\u4e86\u4e00\u5c42\u66f4\u53cb\u597d\u3001\u66f4\u5f3a\u5927\u7684API\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u65b9\u5f0f<\/strong>\uff1a\u540c\u65f6\u652f\u6301XPath\u548cCSS\u9009\u62e9\u5668\uff0c\u5e76\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u94fe\u5f0f\u8c03\u7528\u548c\u6570\u636e\u63d0\u53d6\u65b9\u6cd5\uff08\u5982<code>.get()<\/code>\u3001<code>.getall()<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u96c6LXML\u7684\u6027\u80fd\u548c\u7c7b\u4f3cBeautifulSoup\u7684\u6613\u7528\u6027\u4e8e\u4e00\u8eab\uff0cAPI\u8bbe\u8ba1\u6781\u5176\u9002\u5408\u6570\u636e\u63d0\u53d6\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u77e5\u540d\u5ea6\u76f8\u5bf9\u8f83\u4f4e\uff0c\u5728Scrapy\u751f\u6001\u4e4b\u5916\u4f7f\u7528\u7684\u4eba\u8f83\u5c11\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5982\u4f55\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u89e3\u6790\u5e93\uff1f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f60\u901a\u8fc7<strong>\u7a7f\u4e91API<\/strong>\u62ff\u5230\u5e72\u51c0\u7684HTML\u5b57\u7b26\u4e32<code>html_content<\/code>\u540e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5982\u679c\u4f60\u8ffd\u6c42\u6781\u81f4\u7684\u6027\u80fd\u548c\u6548\u7387<\/strong>\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6d77\u91cf\u9875\u9762\u3001\u6784\u5efa\u9ad8\u6027\u80fd\u722c\u866b\u65f6\uff0c<strong>LXML<\/strong>\u662f\u4f60\u7684\u4e0d\u4e8c\u4e4b\u9009\u3002Python<code># \u4f2a\u4ee3\u7801 from lxml import etree tree = etree.HTML(html_content) titles = tree.xpath('\/\/h1\/text()')<\/code><\/li>\n\n\n\n<li><strong>\u5982\u679c\u4f60\u662f\u65b0\u624b\uff0c\u6216\u8005\u9700\u8981\u5904\u7406\u7684HTML\u975e\u5e38\u4e0d\u89c4\u8303<\/strong>\uff0c\u5e0c\u671b\u4ee5\u6700\u5feb\u7684\u65b9\u5f0f\u4e0a\u624b\u5e76\u5b8c\u6210\u4efb\u52a1\uff0c<strong>BeautifulSoup<\/strong>\u662f\u4f60\u6700\u597d\u7684\u670b\u53cb\u3002Python<code># \u4f2a\u4ee3\u7801 from bs4 import BeautifulSoup soup = BeautifulSoup(html_content, 'lxml') # \u63a8\u8350\u914d\u5408lxml\u89e3\u6790\u5668\u4ee5\u63d0\u5347\u6027\u80fd title = soup.find('h1').get_text()<\/code><\/li>\n\n\n\n<li><strong>\u5982\u679c\u4f60\u662fScrapy\u7528\u6237\uff0c\u6216\u8005\u4f60\u6b23\u8d4fScrapy\u90a3\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u63d0\u53d6\u65b9\u5f0f<\/strong>\uff0c\u90a3\u4e48\u5f3a\u70c8\u63a8\u8350\u4f60\u5355\u72ec\u4f7f\u7528<strong>Parsel<\/strong>\u3002Python<code># \u4f2a\u4ee3\u7801 from parsel import Selector selector = Selector(text=html_content) title = selector.css('h1::text').get()<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7a7f\u4e91API\u4e0e\u89e3\u6790\u5e93\u7684\u5173\u7cfb<\/strong>\uff1a\u7a7f\u4e91API\u8d1f\u8d23\u5e2e\u4f60\u89e3\u51b3\u6700\u96be\u7684\u7f51\u7edc\u8bf7\u6c42\u95ee\u9898\uff0c\u786e\u4fdd\u4f60\u62ff\u5230\u7684\u662f<strong>\u53ef\u4f9b\u89e3\u6790<\/strong>\u7684\u771f\u5b9eHTML\u3002\u800c\u4e0a\u8ff0\u89e3\u6790\u5e93\uff0c\u5219\u8d1f\u8d23\u5e2e\u4f60\u4ece\u8fd9\u4efdHTML\u4e2d\uff0c\u9ad8\u6548\u5730<strong>\u63d0\u53d6\u4ef7\u503c<\/strong>\u3002\u4e24\u8005\u7ed3\u5408\uff0c\u6784\u6210\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u3001\u7aef\u5230\u7aef\u7684\u6570\u636e\u91c7\u96c6\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e09\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u7b54 FAQ<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Q1: \u4e3a\u4ec0\u4e48BeautifulSoup\u7684\u793a\u4f8b\u4e2d\u4e5f\u7528\u4e86<code>lxml<\/code>\uff1f<\/strong> A: BeautifulSoup\u81ea\u8eab\u53ea\u662f\u4e00\u4e2aAPI\u5c01\u88c5\uff0c\u5b83\u9700\u8981\u4e00\u4e2a\u5e95\u5c42\u7684\u89e3\u6790\u5668\u6765\u5de5\u4f5c\u3002\u5b83\u53ef\u4ee5\u914d\u5408Python\u5185\u7f6e\u7684<code>html.parser<\/code>\uff0c\u4f46\u6027\u80fd\u8f83\u5dee\u3002\u5b89\u88c5\u5e76\u6307\u5b9a\u4f7f\u7528<code>lxml<\/code>\u4f5c\u4e3a\u5176\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u5728\u4eab\u53d7BS4\u53cb\u597dAPI\u7684\u540c\u65f6\uff0c\u83b7\u5f97\u63a5\u8fd1LXML\u7684\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>Q2: XPath\u548cCSS\u9009\u62e9\u5668\u54ea\u4e2a\u66f4\u597d\uff1f<\/strong> A: CSS\u9009\u62e9\u5668\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c\u66f4\u9002\u5408\u524d\u7aef\u5f00\u53d1\u8005\uff0c\u80fd\u6ee1\u8db380%\u7684\u63d0\u53d6\u9700\u6c42\u3002XPath\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u80fd\u5904\u7406\u66f4\u590d\u6742\u7684\u8282\u70b9\u5173\u7cfb\uff08\u5982\u7236\u8282\u70b9\u3001\u5144\u5f1f\u8282\u70b9\u9009\u62e9\uff09\uff0c\u5728\u5904\u7406\u590d\u6742HTML\u7ed3\u6784\u65f6\u66f4\u6709\u4f18\u52bf\u3002Parsel\u548cLXML\u4e24\u8005\u90fd\u652f\u6301\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u559c\u597d\u548c\u573a\u666f\u7075\u6d3b\u9009\u62e9\u3002<\/li>\n\n\n\n<li><strong>Q3: \u5982\u679c\u7a7f\u4e91API\u8fd4\u56de\u7684\u662fJSON\u6570\u636e\uff0c\u6211\u5e94\u8be5\u7528\u4ec0\u4e48\u5e93\u89e3\u6790\uff1f<\/strong> A: \u5bf9\u4e8eJSON\u6570\u636e\uff0c\u4f60\u65e0\u9700\u4f7f\u7528\u4e0a\u8ff0\u4efb\u4f55\u5e93\u3002\u76f4\u63a5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u5373\u53ef\uff1a<code>data = json.loads(api_response_text)<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u7ed3\u8bed<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6210\u529f\u7ed5\u8fc7Cloudflare\u53ea\u662f\u7b2c\u4e00\u6b65\uff0c\u9ad8\u6548\u5730\u89e3\u6790\u548c\u63d0\u53d6\u6570\u636e\u540c\u6837\u91cd\u8981\u3002Python\u5f3a\u5927\u7684\u89e3\u6790\u5e93\u751f\u6001\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9009\u62e9\u3002\u5728\u7a7f\u4e91API\u4e3a\u4f60\u626b\u6e05\u6240\u6709\u7f51\u7edc\u969c\u788d\u540e\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u548c\u4e2a\u4eba\u504f\u597d\uff0c\u81ea\u7531\u5730\u9009\u62e9LXML\u3001BeautifulSoup\u6216Parsel\uff0c\u6765\u5b8c\u6210\u6570\u636e\u63d0\u53d6\u8fd9\u6700\u540e\u3001\u4e5f\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\ude80 <strong>\u5728\u6570\u636e\u91c7\u96c6\u7684\u9053\u8def\u4e0a\uff0c\u9700\u8981\u4efb\u4f55\u6280\u672f\u652f\u6301\u5417\uff1f\u8bf7\u7acb\u5373\u901a\u8fc7Telegram\u8054\u7cfb\u6211\u4eec\uff1a@cloudbypasscom<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u606d\u559c\u4f60\uff01\u901a\u8fc7\u4f7f\u7528\u4e13\u4e1a\u7ea7\u53cd\u53cd\u722c\u89e3\u51b3\u65b9\u6848\u2014\u2014\u7a7f\u4e91API\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u5730\u7ed5\u8fc7\u4e86Cloudflare\u7684\u6240\u6709\u9632\u62a4\uff0c\u62ff&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/www.cloudbypass.com\/tutorial\/9946.html\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;\u62ff\u5230HTML\u4e4b\u540e &#8211; \u7528Python\u9ad8\u6548\u89e3\u6790\u7a7f\u4e91API\u8fd4\u56de\u7684\u6570\u636e&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-9946","post","type-post","status-publish","format-standard","hentry","category-cloudflare-unlimited-verification"],"_links":{"self":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/9946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/comments?post=9946"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/9946\/revisions"}],"predecessor-version":[{"id":9957,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/posts\/9946\/revisions\/9957"}],"wp:attachment":[{"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/media?parent=9946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/categories?post=9946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudbypass.com\/tutorial\/wp-json\/wp\/v2\/tags?post=9946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}