{"id":374,"date":"2017-02-05T11:35:44","date_gmt":"2017-02-05T03:35:44","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=374"},"modified":"2017-02-05T11:35:44","modified_gmt":"2017-02-05T03:35:44","slug":"python-article23","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/python-article23\/","title":{"rendered":"\u4f7f\u7528python\u722c\u53d6\u6709\u9053\u9996\u9875\u505a\u4e00\u4e2a\u7ffb\u8bd1\u7a0b\u5e8f"},"content":{"rendered":"<h3>\u4eca\u5929\u60f3\u5199\u4e00\u7bc7\u5173\u4e8epython\u722c\u866b\u7684\u535a\u6587\uff0c\u6b63\u597d\u8fd9\u51e0\u5929\u6709\u51e0\u767e\u4e2a\u5355\u8bcd\u8981\u7ffb\u8bd1\uff0c\u800c\u7ffb\u8bd1\u8f6f\u4ef6\u6211\u6700\u5e38\u7528\u7684\u5c31\u662f\u6709\u9053\uff0c\u53bb\u6709\u9053\u9996\u9875\u6293\u4e2a\u5305\u770b\u4e00\u4e0b\uff0c\u662fhttp\u534f\u8bae\u800c\u4e14\u8fd8\u662fget\u8bf7\u6c42\u8fd9\u5c31\u597d\u529e\u4e86\uff0c\u4f7f\u7528\u65f6\u8981\u6ce8\u610f\u662f\u6709\u9053http:\/\/www.youdao.com\/\uff0c \u4e0d\u662f\u6709\u9053\u7ffb\u8bd1\uff0c\u6709\u9053\u7ffb\u8bd1\u91c7\u7528\u7684\u662fpost\u8bf7\u6c42\uff0c\u8fd9\u6b21\u6211\u4eec\u4f7f\u7528\u6709\u9053\u9996\u9875\uff0c\u4e0b\u9762\u662f\u6211\u6293\u5230\u7684\u5305\u5206\u6790\u7684\u3002<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-375\" src=\"http:\/\/47.93.162.35\/wp-content\/uploads\/2017\/02\/python10.png\" alt=\"\" width=\"1363\" height=\"645\" srcset=\"http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python10.png 1363w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python10-300x142.png 300w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python10-1024x485.png 1024w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python10-768x363.png 768w\" sizes=\"auto, (max-width: 1363px) 100vw, 1363px\" \/><\/p>\n<h3>\u7bad\u5934\u91cc\u6807\u6ce8\u7684\u5c31\u662f\u83b7\u53d6\u5230\u7684\uff0c\u7531\u4e8e\u662fget\u8bf7\u6c42\uff0c\u5f88\u7b80\u5355\uff0c\u4e0d\u9700\u8981\u5176\u4ed6\u4ec0\u4e48\u53c2\u6570\uff0c\u53ea\u770bcontent\u91cc\u7684\u5185\u5bb9\u5373\u53ef\u3002\u4e0b\u9762\u5f00\u59cb\u5199python\u7a0b\u5e8f\uff0c\u65e2\u7136\u53d1\u73b0\u662fhttp\u534f\u8bae\uff0c\u90a3\u6211\u81ea\u5df1\u6709urllib2\u5e93\u53bb\u6293\u5c31\u597d\uff0c\u6211\u4f7f\u7528\u7684\u662fpython2.7\uff0cIDE\u4e3apycharm\uff0c\u4e0b\u9762\u662f\u4ee3\u7801\uff0c\u5206\u4e24\u6bb5\u51fd\u6570\uff0c\u4e0d\u559c\u6b22\u628a\u6240\u6709\u4ee3\u7801\u5199\u5230\u4e00\u8d77\uff0c\u5199\u6210\u51fd\u6570\u4f7f\u7528\u7684\u65f6\u5019\u76f4\u63a5\u8c03\u7528\u5c31\u884c\uff0c\u4e0d\u9700\u8981\u5230\u5904\u627e\uff0c\u5230\u5904\u6ce8\u91ca\u6389\u3002<\/h3>\n<pre><code>#-*- coding=utf8 -*-\nimport urllib2\ndef get_html(url):  #\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\n    result = urllib2.urlopen(url)\n    return result.read()\ndef save_file(text,filename):  #\u4fdd\u5b58\u5230\u6587\u4ef6\n    f = open(filename, 'w')\n    f.write(text)\n    f.close()\nif __name__=='__main__':\n    url = 'http:\/\/www.youdao.com\/w\/result\/#keyfrom=dict2.top'\n    save_file(get_html(url), 'thefile.txt')  #\u5c06\u5f97\u5230\u7684\u7f51\u9875\u76f4\u63a5\u5b58\u5230\u6587\u4ef6\u91cc\n\n\n<\/code><\/pre>\n<h3>\u7136\u540e\u5230\u4f60\u8fd9\u4e2a\u6267\u884c\u7684\u9879\u76ee\u7684\u6587\u4ef6\u5939\u4e0b\u627ethefile.txt\u8fd9\u4e2a\u6587\u4ef6\uff0c\u662f\u6267\u884c\u7684\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u6587\u4ef6\u5939\u91cc\u3002\u6253\u5f00\u53ef\u4ee5\u770b\u5230\u679c\u7136\u83b7\u53d6\u4e86\u5168\u90e8\u7f51\u9875\u5185\u5bb9\u3002<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-376\" src=\"http:\/\/47.93.162.35\/wp-content\/uploads\/2017\/02\/python11.png\" alt=\"\" width=\"1077\" height=\"568\" srcset=\"http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python11.png 1077w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python11-300x158.png 300w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python11-1024x540.png 1024w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python11-768x405.png 768w\" sizes=\"auto, (max-width: 1077px) 100vw, 1077px\" \/><\/p>\n<h4>\u4f46\u662f\u6211\u60f3\u505a\u7684\u662f\u4e00\u4e2a\u7ffb\u8bd1\u7a0b\u5e8f\uff0c\u9700\u8981\u83b7\u53d6\u5177\u4f53\u7684\u610f\u601d\uff0c\u8fd9\u65f6\u5019\u5c31\u8981\u7528\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5173\u952e\u5185\u5bb9\u4e86\uff0c\u73b0\u5728\u5f00\u59cb\u5206\u6790\u6211\u4eec\u6240\u9700\u8981\u7684\u5176\u5b9e\u5c31\u662f\u4e0b\u9762\u8fd9\u4e2a\u6846\u91cc\u7684\u5185\u5bb9<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-377\" src=\"http:\/\/47.93.162.35\/wp-content\/uploads\/2017\/02\/python12.png\" alt=\"\" width=\"502\" height=\"217\" srcset=\"http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python12.png 502w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python12-300x130.png 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/p>\n<h4>\u5176\u5b9e\u5c31\u5728\u7f51\u9875\u8fd9\u91cc,\u5305\u542b\u5728\u4e00\u4e2aul\u4e0b\u9762<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-378\" src=\"http:\/\/47.93.162.35\/wp-content\/uploads\/2017\/02\/python13.png\" alt=\"\" width=\"989\" height=\"381\" srcset=\"http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python13.png 989w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python13-300x116.png 300w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python13-768x296.png 768w\" sizes=\"auto, (max-width: 989px) 100vw, 989px\" \/><\/p>\n<h3>.....\u5199\u4e86\u4e00\u4f1a\u5b8c\u5168\u63d0\u53d6\u4e0d\u51fa\u6765\u8fd9\u5bf9ul\u91cc\u7684\u5185\u5bb9\uff0c\u7f51\u9875\u91cc\u7684ul\u592a\u591a\uff0c\u6ca1\u6709\u660e\u663e\u7684\u7279\u5f81\uff0c\u53ea\u597d\u5f03\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u6211\u7684\u53e6\u5916\u4e00\u4e2a\u795e\u5668\uff0cBeautifulSoup\u5e93\uff0c\u7b80\u76f4\u5c31\u662f\u4e07\u80fd\u7684\u63d0\u53d6\u5e93\u3002\u6ca1\u6709\u5b89\u88c5\u7684\u53ef\u4ee5\u53bb\u5b98\u7f51\u4e0b\u8f7d\uff0c\u5b89\u88c5\u65b9\u5f0f\u53c2\u8003\u6211\u4e0b\u9762\u7684\u8fd9\u7bc7\u535a\u6587\u3002<\/h3>\n<p>\u300b\u300b\u300b<a href=\"https:\/\/pypi.python.org\/pypi\/beautifulsoup4\/4.3.2\" title=\"\u8fd9\u662f\u5b98\u7f51\u4e0b\u8f7d\u5730\u5740\">\u8fd9\u662f\u5b98\u7f51\u4e0b\u8f7d\u5730\u5740<\/a><br \/>\n\u300b\u300b\u300b<a href=\"http:\/\/www.rainweb.site\/index.php\/2017\/02\/05\/python-article22\/\" title=\"\u5f3a\u5927\u7684BeautifulSoup\u5e93\">\u5f3a\u5927\u7684BeautifulSoup\u5e93<\/a><\/p>\n<h3>\u8fd9\u662f\u6211\u63d0\u53d6\u51fa\u6765\u7684\u7ed3\u679c\uff0c\u76f4\u63a5\u5c31\u627e\u5230\u4e86\uff0c\u800c\u4e14\u663e\u793a\u7684\u8fd8\u662f\u4e2d\u6587\uff0c\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e00\u822c\u90fd\u4f1a\u628a\u4e2d\u6587\u7ed9\u7f16\u7801\u4ee5\u540e\u518d\u663e\u793a\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684ul\u662f\u5728\u7b2c\u4e8c\u4e2a\u6240\uff0c\u4ee5\u63d0\u53d6\u51fa\u53bb\u5168\u90e8\u5185\u5bb9\u4ee5\u540e\u76f4\u63a5\u663e\u793a\u7b2c\u4e8c\u4e2a\u3002<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-379\" src=\"http:\/\/47.93.162.35\/wp-content\/uploads\/2017\/02\/python14.png\" alt=\"\" width=\"679\" height=\"603\" srcset=\"http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python14.png 679w, http:\/\/rain1024.com\/wp-content\/uploads\/2017\/02\/python14-300x266.png 300w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><\/p>\n<h3>\u73b0\u5728\u95ee\u9898\u53c8\u6765\u4e86\uff0c\u5982\u4f55\u663e\u793ali\u91cc\u9762\u7684\u5185\u5bb9\uff0c\u6211\u60f3\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u63d0\u53d6\u51fa\u6765\u7684\u662f\u7f16\u7801\u4ee5\u540e\u7684\u4e2d\u6587\uff0c\u53ea\u80fd\u7ee7\u7eed\u4f7f\u7528Beautifulsoup\u8fd9\u4e2a\u795e\u5668\uff0c\u5177\u4f53\u7814\u7a76\u7684\u8fc7\u7a0b\u4e0d\u518d\u8be6\u7ec6\u9610\u8ff0\uff0c\u5927\u5bb6\u770b\u4ee3\u7801\u5c31\u53ef\u4ee5\u3002\u8fd9\u6837\u4ee3\u7801\u57fa\u672c\u5b8c\u6210\uff0c\u4f46\u662f\u4e0d\u65b9\u4fbf\u4f7f\u7528\u554a\uff0curl\u662f\u53ef\u4ee5\u53d8\u5f97\uff0c\u4e8e\u662f\u6211\u5f04\u4e00\u4e2a\u53d8\u91cf\u653e\u8fdb\u53bb\uff0c\u5728\u7ed9\u4e00\u4e2a\u63d0\u793a\u6846\uff0c\u628a\u6bcf\u6b21\u8981\u67e5\u7684\u5355\u8bcd\u8f93\u5165\u5373\u53ef\u3002<\/h3>\n<pre><code class=\"python\">#-*- coding=utf8 -*-\nimport urllib2\nimport re\nfrom bs4 import BeautifulSoup\ndef get_html(url):  #\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\n    result = urllib2.urlopen(url)\n    return result.read()\ndef save_file(text,filename):  #\u4fdd\u5b58\u5230\u6587\u4ef6\n    f = open(filename, 'ab')\n    f.write(text)\n    f.close()\ndef open_file(filename):   #\u6253\u5f00\u6587\u4ef6\n    f = open(filename,'r')\n    text = f.read()\n    f.close()\n    return text\ndef soup_html(text,word):   # \u4f7f\u7528beautifulsoup\u63d0\u53d6\u7684\u51fd\u6570\n    soup = BeautifulSoup(text)\n    result = soup.find_all('ul')  #\u8fd9\u662f\u7b2c\u4e00\u6b21\u63d0\u53d6\u51fa\u6765\u7684ul\u7ed3\u679c\uff0c\u9700\u8981\u8fdb\u884c\u4e0b\u4e00\u6b65\u63d0\u53d6\n    ul_list = str(result[1])   # beautifulsoup\u8981\u6c42\u4f7f\u7528\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u6240\u4ee5\u5148\u8f6c\u6362\u4e00\u4e0b\n    soup_ul = BeautifulSoup(ul_list)  #\u5efa\u7acb\u7b2c\u4e8c\u4e2a\u63d0\u53d6\u5b9e\u4f8b\n    for line in soup_ul.stripped_strings:   #\u5bf9\u83b7\u53d6\u5230\u7684ul\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\n        print line\ndef manage():   #\u5c01\u88c5\u6240\u6709\u51fd\u6570\u7684main\n    word = raw_input('\u8bf7\u8f93\u5165\u8981\u7ffb\u8bd1\u7684\u5355\u8bcd.....')\n    url = 'http:\/\/www.youdao.com\/w\/'+word+'\/#keyfrom=dict2.top'\n    soup_html(get_html(url),word)\nif __name__=='__main__':\n    manage()\n<\/code><\/pre>\n<h3>\u6700\u540e\uff0c\u8fd9\u6b21\u7684\u722c\u53d6\u6bd4\u8f83\u7b80\u5355\uff0c\u662fhttp\u548cget\u8bf7\u6c42\uff0c\u6ca1\u6709\u53c2\u6570\u548c\u5934\u90e8\u4ec0\u4e48\u7684\u8981\u5199\uff0c\u53ea\u662f\u7b80\u5355\u7684\u722c\u53d6\u5185\u5bb9\uff0c\u7136\u540e\u518d\u53bb\u5206\u6790\u63d0\u53d6\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u7ee7\u7eed\u7814\u7a76\uff0c\u8fd9\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5927\u91cf\u5355\u8bcd\u7684\u67e5\u627e\uff0c\u4ee3\u7801\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5982\u679c\u6709\u7591\u95ee\u53ef\u4e0e\u6211\u8054\u7cfb\u3002<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u60f3\u5199\u4e00\u7bc7\u5173\u4e8epython\u722c\u866b\u7684\u535a\u6587\uff0c\u6b63\u597d\u8fd9\u51e0\u5929\u6709\u51e0\u767e\u4e2a\u5355\u8bcd\u8981\u7ffb\u8bd1\uff0c\u800c\u7ffb\u8bd1\u8f6f\u4ef6\u6211\u6700\u5e38\u7528\u7684\u5c31\u662f\u6709\u9053\uff0c\u53bb\u6709\u9053\u9996\u9875\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/python-article23\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":380,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,81],"class_list":["post-374","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\/374","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=374"}],"version-history":[{"count":0,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/380"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}