{"id":486,"date":"2013-10-06T21:58:18","date_gmt":"2013-10-06T13:58:18","guid":{"rendered":"http:\/\/study.5dimn.com\/?p=486"},"modified":"2020-03-30T11:31:40","modified_gmt":"2020-03-30T03:31:40","slug":"%e7%94%a8python%e6%89%b9%e9%87%8f%e4%b8%8b%e8%bd%bd%e8%b1%86%e7%93%a3%e5%b0%8f%e7%ab%99%e7%9a%84%e9%9f%b3%e4%b9%90","status":"publish","type":"post","link":"https:\/\/study.5dimn.com\/?p=486","title":{"rendered":"\u7528Python\u6279\u91cf\u4e0b\u8f7d\u8c46\u74e3\u5c0f\u7ad9\u7684\u97f3\u4e50"},"content":{"rendered":"<p>\u539f\u7406\u5f88\u7b80\u5355\uff0c\u8c46\u74e3\u5c0f\u7ad9\u7684\u7f51\u9875HTML\u770b\u4e86\u4e00\u4e0b\uff0c\u6bcf\u9996\u6b4c\u7684\u540d\u5b57\u548c\u5730\u5740\u90fd\u5199\u597d\u4e86\u5728\u91cc\u9762\uff0c\u53ea\u662f\u6bcf\u6b21\u8f7d\u5165\u5730\u5740\u662f\u4e0d\u4e00\u6837\u7684\u800c\u5df2\u3002\u7528urllib\u8bfb\u53d6\u4e00\u4e0b\uff0c\u5206\u6790\u51fa\u91cc\u9762\u7684\u540d\u5b57\u548c\u5730\u5740\uff0c\u7136\u540e\u4e0b\u8f7d\u5373\u53ef\u3002<\/p>\n<p>\u5148\u770b\u6548\u679c\u56fe\uff0c\u4e0a\u9762\u662f\u547d\u4ee4\u884c\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u5de6\u8fb9\u662f\u4ee3\u7801\uff0c\u53f3\u8fb9\u662f\u62d6\u56de\u6765\u7684\u6587\u4ef6\uff0c\u6210\u5c31\u611f\u6ee1\u6ee1\uff01<\/p>\n<p><a href=\"http:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300.png\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-487 alignnone\" src=\"http:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300-300x187.png\" alt=\"Screenshot - 061013 - 21:53:00\" width=\"300\" height=\"187\" srcset=\"https:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300-300x187.png 300w, https:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300-1024x640.png 1024w, https:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300-480x300.png 480w, https:\/\/study.5dimn.com\/wp-content\/uploads\/2013\/10\/Screenshot-061013-215300.png 1280w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<div class=\"\\&quot;nav-items\\&quot;\">\n<p>\u6700\u540e\u9644\u4e0apython3.2\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; auto-links: false; title: ; notranslate\" title=\"\">\nimport urllib.request\nfrom urllib.request import urlopen\nimport re,os\n\nurl = input(&#039;Please enter the URL of the douban site.\\n(E.g., http:\/\/site.douban.com\/quinta-diminuita\/)\\n&#039;)\ncontent = urlopen(url).read().decode(&#039;utf-8&#039;)\n#\u5206\u6790\u97f3\u4e50\u4eba\u540d\u79f0\u5e76\u521b\u5efa\u76f8\u5e94\u76ee\u5f55\nsite_name = &#039;&#039;.join(re.findall(&#039;&lt;title&gt;\\n\\s*(.*?)&lt;\/title&gt;&#039;, content, re.DOTALL))\nartist = site_name.replace(&#039;\u7684\u5c0f\u7ad9\\n\uff08\u8c46\u74e3\u97f3\u4e50\u4eba\uff09&#039;,&#039;&#039;)\nprint(&#039;Preparing to download all the songs by &#039;+artist+&#039;...&#039;)\nif os.path.exists(&#039;.\/&#039;+artist) == False:\n\tos.mkdir(&#039;.\/&#039;+artist)\n#\u5206\u6790\u51fa\u6240\u6709\u623f\u95f4\u5730\u5740\nroomlist = re.findall(&#039;&lt;div class=\\&quot;nav-items\\&quot;&gt;(.*?)&lt;\\\/div&gt;&#039;, content, re.DOTALL)\nroomurl = re.findall(&#039;href=\\&quot;(.*?)\\&quot;&#039;, roomlist&#x5B;0])\n#\u5bf9\u4e8e\u6bcf\u4e2a\u623f\u95f4\uff0c\u5982\u679c\u6709\u64ad\u653e\u5217\u8868\uff0c\u5219\u5206\u6790\u6bcf\u4e2a\u5217\u8868\u4e2d\u7684\u6b4c\u66f2\u540d\u548c\u5bf9\u5e94\u5730\u5740\uff0c\u7136\u540e\u4e0b\u8f7d\nfor h in roomurl:\n\tcontent = urlopen(h).read().decode(&#039;utf-8&#039;)\n\tplaylist = re.findall(&#039;song_records\\s=\\s\\&#x5B;(.*?)\\]\\;\\n&#039;, content, re.DOTALL)\n\tif playlist != &#x5B;]:\n\t\tfor i in playlist:\n\t\t\tplaylist_spl = re.split(r&#039;},{&#039;, i)\n\t\t\tfor j in playlist_spl:\n\t\t\t\tsong_title = &#039;&#039;.join(re.findall(&#039;\\&quot;name\\&quot;\\:\\&quot;(.*?)\\&quot;&#039;, j))\n\t\t\t\tsong_title = &#039;&#039;.join(song_title).replace(&#039;\\\\&#039;,&#039;&#039;)\n\t\t\t\tsong_title = &#039;&#039;.join(song_title).replace(&#039;\/&#039;,&#039;.&#039;)\n\t\t\t\tsong_url = re.findall(&#039;\\&quot;rawUrl\\&quot;\\:\\&quot;(.*?)\\&quot;&#039;, j)\n\t\t\t\tsong_url_str = &#039;&#039;.join(song_url).replace(&#039;\\\\&#039;,&#039;&#039;)\n\t\t\t\tfilepath = &#039;.\/&#039;+artist+&#039;\/&#039;+song_title+&#039;.mp3&#039;\n\t\t\t\tif os.path.isfile(filepath) == False:\n\t\t\t\t\tprint(&#039;Downloading &#039;+song_title+&#039;...&#039;)\n\t\t\t\t\turllib.request.urlretrieve(song_url_str, filepath)\n\t\t\t\telse:\n\t\t\t\t\tprint(song_title+&#039; alreadly exists. Skipped.&#039;)\nprint(&#039;All the songs have been downloaded (if there are any). Enjoy!&#039;)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u539f\u7406\u5f88\u7b80\u5355\uff0c\u8c46\u74e3\u5c0f\u7ad9\u7684\u7f51\u9875HTML\u770b\u4e86\u4e00\u4e0b\uff0c\u6bcf\u9996\u6b4c\u7684\u540d\u5b57\u548c\u5730\u5740\u90fd\u5199\u597d\u4e86\u5728\u91cc\u9762\uff0c\u53ea\u662f\u6bcf\u6b21\u8f7d\u5165\u5730\u5740\u662f\u4e0d\u4e00\u6837\u7684\u800c\u5df2\u3002\u7528urllib\u8bfb\u53d6\u4e00\u4e0b\uff0c\u5206\u6790\u51fa\u91cc\u9762\u7684\u540d\u5b57\u548c\u5730\u5740\uff0c\u7136\u540e\u4e0b\u8f7d\u5373\u53ef\u3002 \u5148\u770b\u6548\u679c\u56fe\uff0c\u4e0a\u9762\u662f\u547d\u4ee4\u884c\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u5de6\u8fb9\u662f\u4ee3\u7801\uff0c\u53f3\u8fb9\u662f\u62d6\u56de\u6765\u7684\u6587\u4ef6\uff0c\u6210\u5c31\u611f\u6ee1\u6ee1\uff01 \u6700\u540e\u9644\u4e0apython3.2\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-python","entry"],"_links":{"self":[{"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":12,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/study.5dimn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/study.5dimn.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}