{"id":521,"date":"2017-02-14T14:30:07","date_gmt":"2017-02-14T06:30:07","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=521"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"python-article57","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/14\/python-article57\/","title":{"rendered":"\u5173\u4e8e\u300aPython\u5199\u7f51\u7edc\u722c\u866b\u300b\u7b2c\u4e00\u7ae0\u603b\u7ed3"},"content":{"rendered":"<h1>\u5173\u4e8e\u300aPython\u5199\u7f51\u7edc\u722c\u866b\u300b\u7b2c\u4e00\u7ae0\u603b\u7ed3<\/h1>\n<h2>1. \u5173\u4e8e\u722c\u866b\u9519\u8bef\u5904\u7406<\/h2>\n<h3>\u6bd4\u5982\u5728\u722c\u866b\u91cc \u4e0b\u8f7d\u7f51\u9875\u65f6\uff0c \u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u65e0\u6cd5\u63a7\u5236\u7684\u9519\u8bef\uff0c \u6bd4\u5982\u8bf7\u6c42\u7684\u9875\u9762\u53ef\u80fd\u4e0d\u5b58 \u5728\u3002 \u6b64\u65f6\uff0curlli b2 \u4f1a\u629b\u51fa\u5f02\u5e38\uff0c \u7136\u540e\u9000\u51fa\u811a\u672c\u3002\u4e0b\u9762\u518d\u7ed9\u51fa\u4e00\u4e2a\u66f4\u5065\u58ee\u7684\u7248 \u672c\uff0c \u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002<\/h3>\n<pre><code>#-*- coding=utf8 -*-\nimport urllib2\ndef download(url):\n    try:\n        print urllib2.urlopen(url).read()\n    except urllib2.URLError as e:  #\u4e5f\u53ef\u4ee5\u662fHTTPError\u7b49\uff0c\u770b\u5b9e\u9645\u62a5\u9519\u4fee\u6539\n        print \"error\"\n        print e.code   #\u53ef\u4ee5\u6253\u5370\u51fa\u6765\u9519\u8bef\u4ee3\u53f7\u5982404\u3002\n        print e.reason  #\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\nif __name__=='__main__':\n  url = 'http:\/\/www.rainweb.site\/footer.php'\n  download(url)\n<\/code><\/pre>\n<h2>2. \u91cd\u8bd5\u4e0b\u8f7d<\/h2>\n<h3>\u4e0b\u8f7d\u65f6\u9047\u5230\u7684\u9519\u8bef\u7ecf\u5e38 \u662f\u4e34\u65f6\u6027\u7684\uff0c \u6bd4\u5982\u670d\u52a1\u5668 \u8fc7\u8f7d\u65f6\u8fd4\u56de\u7684 503Service Unavailable\u9519\u8bef\u3002\u5bf9\u4e8e\u6b64\u7c7b\u9519\u8bef \u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u4e0b\u8f7d\uff0c\u4e0d\u8fc7\uff0c \u6211\u4eec\u4e0d\u9700\u8981\u5bf9\u6240\u6709\u9519\u8bef\u90fd\u5c1d\u8bd5\u91cd\u65b0\u4e0b\u8f7d\u3002 \u5982\u679c\u670d\u52a1\u5668\u8fd4\u56de\u7684\u662f404 Not Found \u8fd9\u79cd\u9519\u8bef\uff0c\u5219\u8bf4\u660e\u8be5\u7f51\u9875\u76ee\u524d\u5e76\u4e0d\u5b58 \u5728\uff0c\u518d\u6b21\u5c1d\u8bd5 \u540c\u6837\u7684\u8bf7\u6c42\u4e00\u822c \u4e5f\u4e0d\u4f1a\u51fa\u73b0\u4e0d\u540c\u7684\u7ed3\u679c\u30024xx \u9519\u8bef\u53d1\u751f\u5728\u8bf7\u6c42\u5b58\u5728\u95ee\u9898\u65f6\uff0c\u800c5xx \u9519\u8bef\u5219\u53d1\u751f\u5728\u670d\u52a1\u7aef\u5b58\u5728\u95ee\u9898\u65f6\u3002 \u6240\u4ee5\uff0c \u6211\u4eec\u53ea\u9700\u8981\u786e\u4fdddownload\u51fd\u6570\u5728 \u53d1\u751fSxx \u9519\u8bef\u65f6\u91cd\u8bd5\u4e0b\u8f7d \u5373\u53ef\u3002<\/h3>\n<pre><code>#-*- coding=utf8 -*-\nimport urllib2\ndef download(url, num=2):\n    try:\n        print urllib2.urlopen(url).read()\n    except urllib2.URLError as e:\n        print \"error\", e.reason  #\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\n        if num&gt;0:\n            if hasattr(e,'code') and 500&lt;=e.code&lt;600:\n                download(url,num-1)\nif __name__=='__main__':\n  url = 'http:\/\/www.rainweb.site\/footer.php'\n  download(url)\n<\/code><\/pre>\n<h3>\u5f53 download\u51fd\u6570\u9047\u5230Sxx \u9519\u8bef\u7801\u65f6\uff0c \u5c06\u4f1a\u9012\u5f52\u8c03\u7528 \u51fd\u6570\u81ea\u8eab\u8fdb\u884c\u91cd\u8bd5\u3002 \u6b64\u5916\uff0c \u8be5\u51fd\u6570\u8fd8\u589e\u52a0\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c \u7528\u4e8e\u8bbe\u5b9a\u91cd\u8bd5\u4e0b\u8f7d\u7684\u6b21\u6570\uff0c \u5176\u9ed8\u8ba4\u503c\u4e3a\u4e24\u6b21<\/h3>\n<h2>3. \u8bbe\u7f6e\u7528\u6237\u4ee3\u7406<\/h2>\n<p>###  \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0curllib2 \u4f7f\u7528 Python-urllib\/ 2 . 7 \u4f5c\u4e3a\u7528\u6237\u4ee3\u7406\u4e0b\u8f7d\u7f51 \u9875\u5185\u5bb9\uff0c \u5176\u4e2d 2 . 7\u662f Python \u7684\u7248\u672c\u53f7 \u3002User-Agent\uff1apython-urllib2\/.7<\/p>\n<pre><code>User_agent = 'wswp'\nheaders = {'User_agent':user_agent}\nrequest = urllib2.Request(url,headers=headers)\nhtml = urllib2.urlopen(request)\n<\/code><\/pre>\n<h2>4. \u6293\u53d6\u9875\u9762\u4e2d\u6240\u6709\u7b26\u5408\u8981\u6c42\u7684\u94fe\u63a5<\/h2>\n<h3>\u6709\u65f6\u9875\u9762\u4e2d\u8bb8\u591a\u94fe\u63a5\u9700\u8981\u4e0b\u8f7d\uff0c\u5355\u5355\u6539\u53d8url\u4e2d\u51e0\u4e2a\u6570\u5b57\u662f\u65e0\u6cd5\u5168\u90e8\u627e\u5230\u7684\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6839\u636e\u4f60\u6240\u8f93\u5165\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u8981\u6c42\u7684url\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u7f51\u9875\u4e2d\u7684\u76f8\u5bf9\u94fe\u63a5\u8f6c\u6362\u4e3a\u7edd\u5bf9\u94fe\u63a5<\/h3>\n<pre><code>   #-*- coding=utf8 -*-\n   import urllib2\n   import re\n   import urlparse\n   def download(url):\n       try:\n           html = urllib2.urlopen(url).read()\n       except urllib2.URLError as e:\n           print \"error\"\n           print e.code   #\u53ef\u4ee5\u6253\u5370\u51fa\u6765\u9519\u8bef\u4ee3\u53f7\u5982404\u3002\n           print e.reason  #\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\n           html = None\n       return html\n   #\u8981\u6293\u53d6\u7684\u4e3b\u9875\u9762\u548c\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\n   def link_crawler(seed_url,link_regex):\n       crawl_queue = [seed_url]   #\u5c06\u7b2c\u4e00\u4e2aurl\u653e\u5165\n       html = download(seed_url)  #\u83b7\u53d6\u9875\u9762\u6240\u4ee5\u4fe1\u606f\n       for link in get_links(html):#\u904d\u5386\u6240\u5f97\u5230\u7684url\n           if re.match(link_regex, link):#\u5339\u914d\u8f93\u5165\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\n               link = urlparse.urljoin(seed_url,link)\n               print link\n               crawl_queue.append(link)\n       print crawl_queue\n       # for url in crawl_queue:\n       #     html = download(url)\n   def get_links(html):#\u8fd4\u56de\u6240\u9700\u8981\u7684url\n       webpage_regex = re.compile('&lt;a[^&gt;]+href=[\"\\'](.*?)[\"\\']',re.IGNORECASE)\n       # print webpage_regex.findall(html)\n       # exit()\n       return webpage_regex.findall(html)\n   if __name__=='__main__':\n       link_crawler('','')\n<\/code><\/pre>\n<h2>5. \u589e\u52a0\u5ef6\u65f6\u7684\u722c\u866b<\/h2>\n<h2>6. \u589e\u52a0\u9875\u9762\u6df1\u5ea6\uff0c\u907f\u514d\u722c\u866b\u9677\u9631<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u300aPython\u5199\u7f51\u7edc\u722c\u866b\u300b\u7b2c\u4e00\u7ae0\u603b\u7ed3 1. \u5173\u4e8e\u722c\u866b\u9519\u8bef\u5904\u7406 \u6bd4\u5982\u5728\u722c\u866b\u91cc \u4e0b\u8f7d\u7f51\u9875\u65f6\uff0c \u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/14\/python-article57\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,81],"class_list":["post-521","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\/521","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\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=521"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":1483,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/521\/revisions\/1483"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/522"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}