{"id":478,"date":"2017-02-09T14:28:07","date_gmt":"2017-02-09T06:28:07","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=478"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"python-article43","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/09\/python-article43\/","title":{"rendered":"\u4f7f\u7528\u722c\u866b\u722c\u53d6\u8c46\u74e32016\u7535\u5f71\u699c\u5355\u4e2d\u6240\u6709\u7535\u5f71\u4fe1\u606f"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u722c\u866b\u6bcf\u65e5\u7bc7----\u4eca\u5929\u4f7f\u7528\u722c\u866b\u722c\u53d6\u8c46\u74e32016\u7535\u5f71\u699c\u4e0a\u6240\u6709\u7535\u5f71\u4fe1\u606f\uff0c\u672c\u6765\u4ee5\u4e3a\u8c46\u74e3\u8fd9\u79cd\u5927\u793e\u533a\u7684\u9632\u5fa1\u505a\u7684\u4f1a\u5f88\u597d\uff0c\u770b\u5230\u662fHTTPS\u534f\u8bae\uff0c\u6211\u90fd\u51c6\u5907\u5199\u4e00\u5927\u4e32\u5934\u90e8\u53bb\u6a21\u62df\u7528\u6237\u4e86\uff0c\u6ca1\u60f3\u5230\u4e00\u4e2aurlopen\u5c31\u76f4\u63a5\u83b7\u53d6\u4e86\uff0c\u53ef\u80fd\u662f\u7f51\u7ad9\u8bbe\u8ba1\u8005\u6545\u610f\u6ca1\u505a\u7684\u5f88\u5c01\u95ed\uff0c\u8ba9\u6211\u6709\u673a\u53ef\u4e58\u3002\u8fd9\u662f\u7f51\u5740(https:\/\/www.douban.com\/doulist\/3516235\/?start=0&amp;sort=seq&amp;sub_type=)\uff0c\u5927\u5bb6\u53ef\u4ee5\u5148\u770b\u770b\u3002<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/python28.jpg\" alt=\"\" \/><\/p>\n<h3>\u5982\u56fe\uff0c\u8fd9\u5c31\u662f\u7f51\u9875\u7684\u57fa\u672c\u60c5\u51b5\uff0c\u5927\u5bb6\u53ef\u4ee5\u5148\u53bb\u770b\u770b\u7f51\u9875\u6e90\u4ee3\u7801\uff0c\u6211\u73b0\u5728\u8981\u505a\u7684\u5c31\u662f\u628a\u6bcf\u4e2a\u7535\u5f71\u7684\u6574\u4e2a\\&lt;div\\&gt;\u63d0\u53d6\u51fa\u6765,\u4ee3\u7801\u76f4\u63a5\u7528urlopen\u5f04\u4e86\u51fa\u6765\uff0c\u6211\u5148\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc\uff0c\u8981\u6162\u6162\u7684\u6d4b\u8bd5\uff0c\u76f4\u63a5\u7528\u6587\u4ef6\u91cc\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\uff0c\u7701\u5f97\u6bcf\u6b21\u90fd\u6293\u53d6\u9875\u9762\u3002<\/h3>\n<pre><code class=\"python\">&lt;pre class=\"lang:php decode:true \"&gt;# -*- coding: utf-8 -*-\nimport urllib2\nimport re\nfrom bs4 import BeautifulSoup\ndef get_html(url):\nresult = urllib2.urlopen(url)\nreturn result.read()\ndef save_file(text, filename):\nf= open(filename,'w')\nf.write(text)\nf.close()\ndef read_file(filename):\nf = open(filename,'r')\ntext = f.read()\nf.close()\nreturn text\nif __name__=='__main__':\nurl = 'https:\/\/www.douban.com\/doulist\/3516235\/'\nhtml = get_html(url)\nsave_file(html,'thefile.txt')&lt;\/pre&gt;\n<\/code><\/pre>\n<h3>\u4e0b\u4e00\u6b65\u5bf9\u6293\u53d6\u7684\u4ee3\u7801\u5f00\u59cb\u63d0\u53d6\uff0c\u6bcf\u4e2a\u7535\u5f71\u7684\u4ecb\u7ecd\u90fd\u5305\u542b\u5728\u4e00\u5bf9div\u4e2d<\/h3>\n<p>&lt;div class=\"bd doulist-subject\"&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<h3>\u4f7f\u7528BeautifulSoup\u6765\u63d0\u53d6<\/h3>\n<pre><code class=\"python\">html = read_file('thefile.txt')\nsoup = BeautifulSoup(html)\ntext = soup.find_all('div', class_='bd doulist-subject')\nsave_file(str(text),'thefile.txt')\n<\/code><\/pre>\n<p>\u6548\u679c\u5982\u4e0b\u56fe<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/python29.jpg\" alt=\"\" \/><\/p>\n<h3>\u6bcf\u4e2a\u7535\u5f71\u7684\u4fe1\u606f\u90fd\u63d0\u53d6\u51fa\u6765\u4e86\uff0c\u6211\u60f3\u8981\u7684\u662f\u7535\u5f71\u7684\u540d\u79f0\uff0c\u8bc4\u5206\uff0c\u4eba\u5458\uff0c\u4e0a\u6620\u65e5\u671f\uff0c\u6240\u4ee5\u6211\u53ea\u63d0\u53d6\u8fd9\u4e9b\u5185\u5bb9\uff0c\u5927\u5bb6\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u7684\u9700\u6c42\u5199\u3002<\/h3>\n<pre><code class=\"python\">def get_movie_one(movie):\nresult = []\nsoup_all = BeautifulSoup(str(movie))\ntitle = soup_all.find_all('div', class_='title')\nsoup_title = BeautifulSoup(str(title[0]))\nfor line in soup_title.stripped_strings: # \u5bf9\u83b7\u53d6\u5230\u7684&lt;a&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\nprint line\nnum = soup_all.find_all('span', class_='rating_nums')\nprint num[0].contents[0]\ninfo = soup_all.find_all('div', class_='abstract')\nsoup_info = BeautifulSoup(str(info[0]))\nfor line in soup_info.stripped_strings: # \u5bf9\u83b7\u53d6\u5230\u7684&lt;\/a&gt;&lt;a&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\nprint line&lt;\/a&gt;\n\n## \u7ed3\u679c\uff1a\n\u4e00\u5207\u90fd\u597d\n6.4\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\n<\/code><\/pre>\n<h3>\u7b2c\u4e00\u4e2a\u5df2\u7ecf\u6210\u529f\u4e86\uff0c\u73b0\u5728\u5c31\u5f00\u59cb\u6279\u91cf\u7684\u64cd\u4f5c\uff0c\u4e00\u5171\u6709425\u4e2a\u7535\u5f71\uff0c\u6211\u6bcf\u63d0\u53d6\u4e00\u4e2a\u5c31\u5b58\u5230\u6587\u4ef6\u91cc\uff0c\u8fd9\u662f\u7b2c\u4e00\u987525\u4e2a\u7535\u5f71\u63d0\u53d6\u51fa\u6765\u7684\u6548\u679c\u3002<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/python30.jpg\" alt=\"\" \/><\/p>\n<p>\u4e0b\u9762\u662f\u5168\u90e8\u7684\u4ee3\u7801\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/env python\n# -*- coding=utf-8 -*-\nimport sys\nreload(sys)\nsys.setdefaultencoding( \"utf-8\" )\nimport urllib2\nimport re\nimport time\nfrom bs4 import BeautifulSoup\ndef get_html(url):  #\u901a\u8fc7url\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\n    result = urllib2.urlopen(url)\n    return result.read()\n    # save_file(result.read(), 'thefile.txt')\ndef get_movie_all(html):     #\u901a\u8fc7soup\u63d0\u53d6\u5230\u6bcf\u4e2a\u7535\u5f71\u7684\u5168\u90e8\u4fe1\u606f\uff0c\u4ee5list\u8fd4\u56de\n    soup = BeautifulSoup(html)\n    movie_list = soup.find_all('div', class_='bd doulist-subject')\n    return movie_list\ndef get_movie_one(movie):\n    result = []  # \u7528\u4e8e\u5b58\u50a8\u63d0\u53d6\u51fa\u6765\u7684\u7535\u5f71\u4fe1\u606f\n    soup_all = BeautifulSoup(str(movie))\n    title = soup_all.find_all('div', class_='title')\n    soup_title = BeautifulSoup(str(title[0]))\n    for line in soup_title.stripped_strings:  # \u5bf9\u83b7\u53d6\u5230\u7684&lt;a&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\n        result.append(line)\n\n    # num = soup_all.find_all('span', class_='rating_nums')\n    num = soup_all.find_all('span')\n    result.append(num[1].contents[0])\n\n    soup_num = BeautifulSoup(str(num[0]))\n    for line in soup_num.stripped_strings:  # \u5bf9\u83b7\u53d6\u5230\u7684&lt;span&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\n        result = result + line\n\n    info = soup_all.find_all('div', class_='abstract')\n    soup_info = BeautifulSoup(str(info[0]))\n    result_str = \"\"\n    for line in soup_info.stripped_strings:  # \u5bf9\u83b7\u53d6\u5230\u7684&lt;div&gt;\u91cc\u7684\u5185\u5bb9\u8fdb\u884c\u63d0\u53d6\n        result_str = result_str + line\n    result.append(result_str)\n    return result  #\u8fd4\u56de\u83b7\u53d6\u5230\u7684\u7ed3\u679c\ndef save_file(text, filename):  #\u4fdd\u5b58\u7f51\u9875\u5230\u6587\u4ef6\n    f= open(filename,'ab')\n    f.write(text)\n    f.close()\ndef read_file(filename):  #\u8bfb\u53d6\u6587\u4ef6\n    f = open(filename,'r')\n    text = f.read()\n    f.close()\n    return text\n\nif __name__=='__main__':\n    for i in range(0,426,25):\n        url = 'https:\/\/www.douban.com\/doulist\/3516235\/?start='+str(i)+'&amp;sort=seq&amp;sub_type='\n        html = get_html(url)\n        movie_list = get_movie_all(html)\n        for movie in movie_list:  #\u5c06\u6bcf\u4e00\u9875\u4e2d\u7684\u6bcf\u4e2a\u7535\u5f71\u4fe1\u606f\u653e\u5165\u51fd\u6570\u4e2d\u63d0\u53d6\n            result = get_movie_one(movie)\n            text = ''+'\u7535\u5f71\u540d\uff1a'+str(result[0])+' | \u8bc4\u5206\uff1a'+str(result[1])+' | '+str(result[2])+'\\n'+'\\t'\n            save_file(text,'thee.txt')\n        time.sleep(5)  #\u6bcf\u96945\u79d2\u6293\u53d6\u4e00\u9875\u7684\u4fe1\u606f\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u722c\u866b\u6bcf\u65e5\u7bc7&#8212;-\u4eca\u5929\u4f7f\u7528\u722c\u866b\u722c\u53d6\u8c46\u74e32016\u7535\u5f71\u699c\u4e0a\u6240\u6709\u7535\u5f71\u4fe1\u606f\uff0c\u672c\u6765\u4ee5\u4e3a\u8c46\u74e3\u8fd9\u79cd\u5927\u793e\u533a\u7684\u9632\u5fa1\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/09\/python-article43\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":480,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,81],"class_list":["post-478","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\/478","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=478"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":1497,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions\/1497"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/480"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}