{"id":373,"date":"2017-02-05T11:24:16","date_gmt":"2017-02-05T03:24:16","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=373"},"modified":"2017-02-05T11:24:16","modified_gmt":"2017-02-05T03:24:16","slug":"python-article22","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/python-article22\/","title":{"rendered":"\u5f3a\u5927\u7684BeautifulSoup\u5e93"},"content":{"rendered":"<h2>BeautifulSoup\u7b80\u76f4\u5c31\u662f\u795e\u5668\uff0c\u7701\u53bb\u4e86\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u800c\u4e14\u8fd8\u80fd\u81ea\u52a8\u5c06\u5b57\u7b26\u7f16\u7801\uff0c\u4e0b\u9762\u662f\u5173\u4e8e\u6211\u5728\u722c\u866b\u4e2d\u4f7f\u7528BeautifulSoup\u5e93\u7684\u603b\u7ed3\uff0c\u6700\u5e38\u7528\uff0c\u6700\u597d\u7528\u7684\u4e00\u4e9b\u529f\u80fd\u3002\u4e0b\u9762\u4f1a\u628a\u6211\u4f7f\u7528\u7684\u793a\u4f8bHTML\u4ee3\u7801\u653e\u51fa\u6765\u3002<\/h2>\n<h2>\u4e3a\u4e86\u683c\u5f0f\u6574\u9f50\uff0c\u90fd\u5199\u5728\u4ee3\u7801\u91cc\u4e86<\/h2>\n<pre><code>\u5bfc\u5165\u5e93\uff1a from bs4 import BeautifulSoup\n\u5b9e\u4f8b\u5316\uff1a soup = BeautifulSoup(text)\n\n1. \u6700\u5e38\u7528\u7684\u529f\u80fd:soup.title,\u8868\u793a\u67e5\u627e\u5230title\u6807\u7b7e\uff0c\u6216\u8005soup.div\uff0c\u6ce8\u610f\u6b64\u65f6\u8f93\u51fa\u7684\u503c\u662f\u662f\u6574\u4e2a\u6807\u7b7e\uff0c\u7ed3\u679c\uff1a\u30102016\u5e74\u56fd\u5185\u516c\u6620\u7535\u5f71\u6392\u671f\u3011\uff0c\u5982\u679c\u6b64\u65f6\u4f60\u60f3\u8981\u83b7\u53d6\u91cc\u9762\u7684\u5185\u5bb9\uff0c\u5c31\u8981\u8fd9\u6837\u5199:soup.title.string\uff0c\u7ed3\u679c\uff1a\u30102016\u5e74\u56fd\u5185\u516c\u6620\u7535\u5f71\u6392\u671f\u3011\uff0c\u4f46\u662f\u4f60\u8981\u786e\u5b9a\u6807\u7b7e\u91cc\u6709\u5b57\u7b26\uff0c\u5982\u679c\u91cc\u9762\u662f\u4e00\u5c42\u5957\u4e00\u5c42\u7684\u6807\u7b7e\uff0c\u5c31\u83b7\u53d6\u4e0d\u5230\u5185\u5bb9\uff0c\u8fd4\u56denone\uff0c\u5982\u679c\u4e3e\u4e00\u53cd\u4e09\uff0c\u5176\u4ed6\u6807\u7b7e\u91cc\u4e5f\u662f\u8fd9\u6837\u7684\u3002\n\n2. \u83b7\u53d6\u6807\u7b7e\u91cc\u7684\u5c5e\u6027\u503c\uff0c\u5982\u679c\u73b0\u5728\u6709\u8fd9\u6837\u4e00\u884c\u6587\u672c\uff1a\n\n\uff0c\u6211\u60f3\u8981\u83b7\u53d6class\u7684\u503c\uff1asoup.div['class']\uff0c\u5f97\u5230\u7ed3\u679c\uff1a[u'global-nav-items'],\u53ef\u4ee5\u770b\u51fa\u6765\u8fd9\u662f\u4e2a\u5217\u8868\uff0c\u6211\u60f3\u8981\u7684\u662f\u503c\uff0c\u6240\u4ee5\u518d\u52a0\u4e00\u6b65\uff1asoup.div['class'][0],\u5c31\u80fd\u5f97\u5230\uff1aglobal-nav-items\n\n3. \u597d\u7528\u7684contents\uff0c\u5728\u63d0\u53d6\u65f6\u6709\u65f6\u4f1a\u78b0\u5230\u591a\u5c42\u5d4c\u5957\u7684\u6807\u7b7e\uff0c\u6bd4\u5982\u8fd9\u6837\u7684\uff1a\n\n\u66f4\u591a\n\uff0c\u4e09\u5c42\u5d4c\u5957\u5728\u4e00\u8d77\uff0c\u800c\u6211\u60f3\u5f97\u5230\u2019\u66f4\u591a\u2018\u8fd9\u4e2a\u5185\u5bb9\uff0c\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a\nre = soup.find_all('li') | print re[0].contents[1].contents[1].string\n\u5982\u679c\u4f7f\u7528\u7684\u662fsoup.li\uff0c\u5219re\u540e\u6ca1\u6709[]\uff0c\u5f88\u597d\u7406\u89e3\uff0c\u4e09\u5c42\u5d4c\u5957\u6240\u4ee5\u662f\u672c\u4f53\u52a0\u4e24\u4e2acontens\uff0c\u6700\u540e\u7528string\u5f97\u5230\u5185\u5bb9\u3002\n\n4. \u67e5\u627e\u6307\u5b9a\u6807\u7b7e\uff0c\u6bd4\u5982\n\n\uff0c\u6211\u8981\u627e\u5230\u8fd9\u4e2a\u6807\u7b7e\u5c31\u5199\u6210\uff1aprint soup.find_all('div', class_='abstract')\uff0c\u8fd9\u662f\u67e5\u627e\u591a\u4e2a\uff0c\u5982\u679c\u67e5\u627e\u4e00\u4e2a\uff0c\u5219\u4f7f\u7528find\u3002\n\n5. \u63d0\u53d6\u6bd4\u8f83\u591a\u7684\u800c\u4e14\u4e0d\u89c4\u5219\u7684\u6807\u7b7e\u5185\u5bb9\uff0c\u5728\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u6211\u5df2\u7ecf\u67e5\u5230\u6307\u5b9a\u6807\u7b7e\uff0c\u800c\u8fd9\u4e2a\u6807\u7b7e\u91cc\u7684\u5185\u5bb9\u662f\u8fd9\u6837\u7684\uff0c\n\n\u5bfc\u6f14: \u5f20\u731b\n\n\u4e3b\u6f14: \u5f20\u56fd\u7acb \/ \u59da\u6668 \/ \u7aa6\u9a81\n\n\u7c7b\u578b: \u5267\u60c5 \/ \u5bb6\u5ead\n\n\u5236\u7247\u56fd\u5bb6\/\u5730\u533a: \u4e2d\u56fd\u5927\u9646\n\n\u5e74\u4efd: 2016\n\n\u6709\u7a7a\u683c\uff0c\u6709\u6362\u884c\u7b26\uff0c\u8fd9\u4e2a\u5185\u5bb9\u5c31\u5f88\u4e71\u4e86\uff0c\u7528soup.find('div', class_='abstract').string\u5df2\u7ecf\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u4e8e\u662f\u4f7f\u7528\u4e0b\u9762\u8fd9\u5f20\u65b9\u6cd5\uff0c\u5b8c\u7f8e\u89e3\u51b3\uff0c\u63d0\u53d6\u5230\u7684\u5185\u5bb9\u5f88\u6574\u9f50\u3002\ninfo = soup.find_all('div', class_='abstract')\nsoup_info = BeautifulSoup(str(info[0])) # beautifulsoup\u8981\u6c42\u4f7f\u7528\u5b57\u7b26\u4e32\u7c7b\u578b\nfor line in soup_info.stripped_strings: #\u5bf9&lt;div&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\nprint line\n\u7ed3\u679c\uff1a\n\u5bfc\u6f14: \u5f20\u731b\n\u4e3b\u6f14: \u5f20\u56fd\u7acb \/ \u59da\u6668 \/ \u7aa6\u9a81\n\u7c7b\u578b: \u5267\u60c5 \/ \u5bb6\u5ead\n\u5236\u7247\u56fd\u5bb6\/\u5730\u533a: \u4e2d\u56fd\u5927\u9646\n\u5e74\u4efd: 2016\n6.&lt;\/pre&gt;\n<\/code><\/pre>\n<div class=\"global-nav-items\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>BeautifulSoup\u7b80\u76f4\u5c31\u662f\u795e\u5668\uff0c\u7701\u53bb\u4e86\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u800c\u4e14\u8fd8\u80fd\u81ea\u52a8\u5c06\u5b57\u7b26\u7f16\u7801\uff0c\u4e0b\u9762\u662f\u5173\u4e8e\u6211\u5728\u722c\u866b\u4e2d\u4f7f\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/python-article22\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,81],"class_list":["post-373","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","tag-python","tag-reptile","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":0,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}