{"id":4556,"date":"2022-03-27T11:02:05","date_gmt":"2022-03-27T03:02:05","guid":{"rendered":"https:\/\/egonlin.com\/?p=4556"},"modified":"2022-03-27T11:02:05","modified_gmt":"2022-03-27T03:02:05","slug":"%e7%ac%ac%e4%b8%89%e7%ab%a0%ef%bc%9a%e8%a7%a3%e6%9e%90%e5%ba%93beautifulsoup","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4556","title":{"rendered":"\u7b2c\u4e09\u7ae0\uff1a\u89e3\u6790\u5e93beautifulsoup"},"content":{"rendered":"<p>\u4e00 \u4ecb\u7ecd<br \/>\nBeautiful Soup \u662f\u4e00\u4e2a\u53ef\u4ee5\u4eceHTML\u6216XML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u7684Python\u5e93.\u5b83\u80fd\u591f\u901a\u8fc7\u4f60\u559c\u6b22\u7684\u8f6c\u6362\u5668\u5b9e\u73b0\u60ef\u7528\u7684\u6587\u6863\u5bfc\u822a,\u67e5\u627e,\u4fee\u6539\u6587\u6863\u7684\u65b9\u5f0f.Beautiful Soup\u4f1a\u5e2e\u4f60\u8282\u7701\u6570\u5c0f\u65f6\u751a\u81f3\u6570\u5929\u7684\u5de5\u4f5c\u65f6\u95f4.\u4f60\u53ef\u80fd\u5728\u5bfb\u627e Beautiful Soup3 \u7684\u6587\u6863,Beautiful Soup 3 \u76ee\u524d\u5df2\u7ecf\u505c\u6b62\u5f00\u53d1,\u5b98\u7f51\u63a8\u8350\u5728\u73b0\u5728\u7684\u9879\u76ee\u4e2d\u4f7f\u7528Beautiful Soup 4, \u79fb\u690d\u5230BS4<\/p>\n<h1>\u5b89\u88c5 Beautiful Soup<\/h1>\n<p>pip install beautifulsoup4<\/p>\n<h1>\u5b89\u88c5\u89e3\u6790\u5668<\/h1>\n<p>Beautiful Soup\u652f\u6301Python\u6807\u51c6\u5e93\u4e2d\u7684HTML\u89e3\u6790\u5668,\u8fd8\u652f\u6301\u4e00\u4e9b\u7b2c\u4e09\u65b9\u7684\u89e3\u6790\u5668,\u5176\u4e2d\u4e00\u4e2a\u662f lxml .\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u540c,\u53ef\u4ee5\u9009\u62e9\u4e0b\u5217\u65b9\u6cd5\u6765\u5b89\u88c5lxml:<\/p>\n<p>$ apt-get install Python-lxml<\/p>\n<p>$ easy_install lxml<\/p>\n<p>$ pip install lxml<\/p>\n<p>\u53e6\u4e00\u4e2a\u53ef\u4f9b\u9009\u62e9\u7684\u89e3\u6790\u5668\u662f\u7eafPython\u5b9e\u73b0\u7684 html5lib , html5lib\u7684\u89e3\u6790\u65b9\u5f0f\u4e0e\u6d4f\u89c8\u5668\u76f8\u540c,\u53ef\u4ee5\u9009\u62e9\u4e0b\u5217\u65b9\u6cd5\u6765\u5b89\u88c5html5lib:<\/p>\n<p>$ apt-get install Python-html5lib<\/p>\n<p>$ easy_install html5lib<\/p>\n<p>$ pip install html5lib<br \/>\n\u4e0b\u8868\u5217\u51fa\u4e86\u4e3b\u8981\u7684\u89e3\u6790\u5668,\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9,\u5b98\u7f51\u63a8\u8350\u4f7f\u7528lxml\u4f5c\u4e3a\u89e3\u6790\u5668,\u56e0\u4e3a\u6548\u7387\u66f4\u9ad8. \u5728Python2.7.3\u4e4b\u524d\u7684\u7248\u672c\u548cPython3\u4e2d3.2.2\u4e4b\u524d\u7684\u7248\u672c,\u5fc5\u987b\u5b89\u88c5lxml\u6216html5lib, \u56e0\u4e3a\u90a3\u4e9bPython\u7248\u672c\u7684\u6807\u51c6\u5e93\u4e2d\u5185\u7f6e\u7684HTML\u89e3\u6790\u65b9\u6cd5\u4e0d\u591f\u7a33\u5b9a.<\/p>\n<p>\u89e3\u6790\u5668 \u4f7f\u7528\u65b9\u6cd5    \u4f18\u52bf  \u52a3\u52bf<br \/>\nPython\u6807\u51c6\u5e93   BeautifulSoup(markup, &quot;html.parser&quot;)<br \/>\nPython\u7684\u5185\u7f6e\u6807\u51c6\u5e93<br \/>\n\u6267\u884c\u901f\u5ea6\u9002\u4e2d<br \/>\n\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5f3a<br \/>\nPython 2.7.3 or 3.2.2)\u524d \u7684\u7248\u672c\u4e2d\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5dee<br \/>\nlxml HTML \u89e3\u6790\u5668   BeautifulSoup(markup, &quot;lxml&quot;)<br \/>\n\u901f\u5ea6\u5feb<br \/>\n\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5f3a<br \/>\n\u9700\u8981\u5b89\u88c5C\u8bed\u8a00\u5e93<br \/>\nlxml XML \u89e3\u6790\u5668<br \/>\nBeautifulSoup(markup, [&quot;lxml&quot;, &quot;xml&quot;])<\/p>\n<p>BeautifulSoup(markup, &quot;xml&quot;)<\/p>\n<p>\u901f\u5ea6\u5feb<br \/>\n\u552f\u4e00\u652f\u6301XML\u7684\u89e3\u6790\u5668<br \/>\n\u9700\u8981\u5b89\u88c5C\u8bed\u8a00\u5e93<br \/>\nhtml5lib    BeautifulSoup(markup, &quot;html5lib&quot;)<br \/>\n\u6700\u597d\u7684\u5bb9\u9519\u6027<br \/>\n\u4ee5\u6d4f\u89c8\u5668\u7684\u65b9\u5f0f\u89e3\u6790\u6587\u6863<br \/>\n\u751f\u6210HTML5\u683c\u5f0f\u7684\u6587\u6863<br \/>\n\u901f\u5ea6\u6162<br \/>\n\u4e0d\u4f9d\u8d56\u5916\u90e8\u6269\u5c55<br \/>\n\u4e2d\u6587\u6587\u6863\uff1a<a href=\"https:\/\/www.crummy.com\/software\/BeautifulSoup\/bs4\/doc\/index.zh.html\">https:\/\/www.crummy.com\/software\/BeautifulSoup\/bs4\/doc\/index.zh.html<\/a><\/p>\n<p>\u4e8c \u57fa\u672c\u4f7f\u7528<br \/>\nhtml_doc = &quot;&quot;&quot;<\/p>\n<p><html><head><title>The Dormouse&#8217;s story<\/title><\/head><br \/>\n<body><\/p>\n<p class=\"title\"><b>The Dormouse&#8217;s story<\/b><\/p>\n<p class=\"story\">Once upon a time there were three little sisters; and their names were<br \/>\n<a href=\"http:\/\/example.com\/elsie\" class=\"sister\" id=\"link1\">Elsie<\/a>,<br \/>\n<a href=\"http:\/\/example.com\/lacie\" class=\"sister\" id=\"link2\">Lacie<\/a> and<br \/>\n<a href=\"http:\/\/example.com\/tillie\" class=\"sister\" id=\"link3\">Tillie<\/a>;<br \/>\nand they lived at the bottom of a well.<\/p>\n<p class=\"story\">&#8230;<\/p>\n<p>&#8220;&#8221;&#8221;<\/p>\n<p>#\u57fa\u672c\u4f7f\u7528\uff1a\u5bb9\u9519\u5904\u7406,\u6587\u6863\u7684\u5bb9\u9519\u80fd\u529b\u6307\u7684\u662f\u5728html\u4ee3\u7801\u4e0d\u5b8c\u6574\u7684\u60c5\u51b5\u4e0b,\u4f7f\u7528\u8be5\u6a21\u5757\u53ef\u4ee5\u8bc6\u522b\u8be5\u9519\u8bef\u3002\u4f7f\u7528BeautifulSoup\u89e3\u6790\u4e0a\u8ff0\u4ee3\u7801,\u80fd\u591f\u5f97\u5230\u4e00\u4e2a BeautifulSoup \u7684\u5bf9\u8c61,\u5e76\u80fd\u6309\u7167\u6807\u51c6\u7684\u7f29\u8fdb\u683c\u5f0f\u7684\u7ed3\u6784\u8f93\u51fa<br \/>\nfrom bs4 import BeautifulSoup<br \/>\nsoup=BeautifulSoup(html_doc,&#8217;lxml&#8217;) #\u5177\u6709\u5bb9\u9519\u529f\u80fd<br \/>\nres=soup.prettify() #\u5904\u7406\u597d\u7f29\u8fdb\uff0c\u7ed3\u6784\u5316\u663e\u793a<br \/>\nprint(res)<br \/>\n\u4e09 \u904d\u5386\u6587\u6863\u6811<br \/>\n#\u904d\u5386\u6587\u6863\u6811\uff1a\u5373\u76f4\u63a5\u901a\u8fc7\u6807\u7b7e\u540d\u5b57\u9009\u62e9\uff0c\u7279\u70b9\u662f\u9009\u62e9\u901f\u5ea6\u5feb\uff0c\u4f46\u5982\u679c\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u6807\u7b7e\u5219\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a<br \/>\n#1\u3001\u7528\u6cd5<br \/>\n#2\u3001\u83b7\u53d6\u6807\u7b7e\u7684\u540d\u79f0<br \/>\n#3\u3001\u83b7\u53d6\u6807\u7b7e\u7684\u5c5e\u6027<br \/>\n#4\u3001\u83b7\u53d6\u6807\u7b7e\u7684\u5185\u5bb9<br \/>\n#5\u3001\u5d4c\u5957\u9009\u62e9<br \/>\n#6\u3001\u5b50\u8282\u70b9\u3001\u5b50\u5b59\u8282\u70b9<br \/>\n#7\u3001\u7236\u8282\u70b9\u3001\u7956\u5148\u8282\u70b9<br \/>\n#8\u3001\u5144\u5f1f\u8282\u70b9<br \/>\nView Code<br \/>\n\u56db \u641c\u7d22\u6587\u6863\u6811<br \/>\n1\u3001\u4e94\u79cd\u8fc7\u6ee4\u5668<\/p>\n<p>View Code<br \/>\n2\u3001find_all( name , attrs , recursive , text , **kwargs )<\/p>\n<p>View Code<br \/>\n3\u3001find( name , attrs , recursive , text , **kwargs )<\/p>\n<p>View Code<br \/>\n4\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<p>View Code<br \/>\n5\u3001CSS\u9009\u62e9\u5668<\/p>\n<p>View Code<br \/>\n\u4e94 \u4fee\u6539\u6587\u6863\u6811<br \/>\n\u94fe\u63a5\uff1ahttps:\/\/www.crummy.com\/software\/BeautifulSoup\/bs4\/doc\/index.zh.html#id40<\/p>\n<p>\u516d \u603b\u7ed3<br \/>\n# \u603b\u7ed3:<br \/>\n#1\u3001\u63a8\u8350\u4f7f\u7528lxml\u89e3\u6790\u5e93<br \/>\n#2\u3001\u8bb2\u4e86\u4e09\u79cd\u9009\u62e9\u5668:\u6807\u7b7e\u9009\u62e9\u5668,find\u4e0efind_all\uff0ccss\u9009\u62e9\u5668<br \/>\n    1\u3001\u6807\u7b7e\u9009\u62e9\u5668\u7b5b\u9009\u529f\u80fd\u5f31,\u4f46\u662f\u901f\u5ea6\u5feb<br \/>\n    2\u3001\u5efa\u8bae\u4f7f\u7528find,find_all\u67e5\u8be2\u5339\u914d\u5355\u4e2a\u7ed3\u679c\u6216\u8005\u591a\u4e2a\u7ed3\u679c<br \/>\n    3\u3001\u5982\u679c\u5bf9css\u9009\u62e9\u5668\u975e\u5e38\u719f\u6089\u5efa\u8bae\u4f7f\u7528select<br \/>\n#3\u3001\u8bb0\u4f4f\u5e38\u7528\u7684\u83b7\u53d6\u5c5e\u6027attrs\u548c\u6587\u672c\u503cget_text()\u7684\u65b9\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00 \u4ecb\u7ecd Beautiful Soup \u662f\u4e00\u4e2a\u53ef\u4ee5\u4eceHTML\u6216XML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u7684Python\u5e93.\u5b83\u80fd\u591f\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[342,348],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4556"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4556"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4556\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}