{"id":836,"date":"2017-08-03T21:28:17","date_gmt":"2017-08-03T13:28:17","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=836"},"modified":"2023-08-07T20:57:27","modified_gmt":"2023-08-07T12:57:27","slug":"article91","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/08\/03\/article91\/","title":{"rendered":"\u81ea\u52a8\u68c0\u6d4bCSDN\u535a\u5ba2\u6587\u7ae0\u9605\u8bfb\u6b21\u6570\u7684\u722c\u866b"},"content":{"rendered":"<h2>\u81ea\u52a8\u68c0\u6d4bCSDN\u535a\u5ba2\u6587\u7ae0\u9605\u8bfb\u6b21\u6570\u7684\u722c\u866b<\/h2>\n<h3>\u5e73\u65f6\u6ca1\u4e8b\u4f1a\u53bbCSDN\u4e0a\u770b\u4e00\u770b\u535a\u5ba2\u91cc\u6709\u4ec0\u4e48\u53d8\u5316\uff0c\u5982\u679c\u6587\u7ae0\u6709\u8bc4\u8bba\u6216\u8005\u6709\u4e86\u79c1\u4fe1\uff0cCSDN\u4e0a\u90fd\u4e0d\u4f1a\u901a\u77e5\u4f60\uff0c\u8fd9\u5c31\u9700\u8981\u7ecf\u5e38\u67e5\u770b\u535a\u5ba2\u4fe1\u606f\u3002\u5176\u5b9e\u8fd9\u79cd\u4e8b\u60c5\u5b8c\u5168\u53ef\u4ee5\u5199\u4e00\u4e2a\u811a\u672c\u6765\u81ea\u52a8\u68c0\u6d4b\u8bc6\u522b\uff0c\u5982\u679c\u535a\u5ba2\u91cc\u7684\u4fe1\u606f\u6709\u4ec0\u4e48\u53d8\u5316\uff0c\u81ea\u52a8\u53d1\u4e00\u4e2a\u90ae\u4ef6\u7ed9\u6211\uff0c\u5e76\u4e14\u544a\u8bc9\u6211\u54ea\u4e9b\u4fe1\u606f\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u518d\u5c06\u8fd9\u4e2a\u811a\u672c\u653e\u5230\u8ba1\u5212\u4efb\u52a1\u4e2d\uff0c\u6bcf\u5929\u5b9a\u65f6\u6267\u884c\u626b\u63cf\u535a\u5ba2\u5185\u5bb9\u3002<\/h3>\n<h3>\u4e0b\u9762\u5177\u4f53\u8bf4\u660e\u4e00\u4e0b\u8fd9\u4e2a\u811a\u672c\u7684\u4f5c\u7528\u548c\u6ce8\u610f\uff1a<\/h3>\n<h4>1.  \u200b\u6211\u7684\u60f3\u6cd5\u662f\u5148\u5c06\u6bcf\u7bc7\u6587\u7ae0\u7684\u9605\u8bfb\u6b21\u6570\u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/h4>\n<h4>2.  \u7136\u540e\u6bcf\u6b21\u5c06\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e\u4e0e\u535a\u5ba2\u91cc\u7684\u6570\u636e\u8fdb\u884c\u5bf9\u6bd4<\/h4>\n<h4>3.  \u518d\u5c06\u6709\u5dee\u522b\u7684\u8bb0\u5f55\u4e0b\u6765\uff0c\u5177\u4f53\u4fe1\u606f\u653e\u5230\u90ae\u4ef6\u4e2d\u53d1\u9001\u7ed9\u6211<\/h4>\n<h4>4.  URL\u4e2dlist\u540e\u9762\u662f\u9875\u6570\uff0c\u5f53\u9875\u6570\u7279\u522b\u5927\u65f6\u5c31\u663e\u793a\u6240\u6709\u6587\u7ae0\uff0c\u4e0d\u7528\u5206\u9875<\/h4>\n<h4>5.  \u6587\u7ae0\u4fe1\u606f\u6458\u53d6\u4f7f\u7528BeautifulSoup\u5e93\uff0c\u5d4c\u5957\u4e86\u51e0\u5c42\u7ec8\u4e8e\u63d0\u53d6\u4e86\u6240\u6709\u4fe1\u606f<\/h4>\n<h4>6.  \u5199\u5b8c\u811a\u672c\u653e\u5230\u670d\u52a1\u5668\u91cc\uff0c\u5f00\u4e00\u4e2a\u8ba1\u5212\u4efb\u52a1\uff0c\u5199\u4e00\u4e2aShell\u8ba9\u5b83\u81ea\u52a8\u6267\u884c\u5c31\u597d\u4e86<\/h4>\n<h3>\u4e0b\u9762\u662f\u6e90\u7801\uff1a<\/h3>\n<pre><code># -*- coding=utf-8 -*-\nimport sys\nreload(sys)\nsys.setdefaultencoding( \"utf-8\" )\nimport urllib2\nimport MySQLdb\nimport time\nfrom bs4 import BeautifulSoup\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.header import Header\ndef 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\ndef email(text ,toemail):\n    sender = \"rain@rain1024.com\"   #\u53d1\u9001\u65b9\u7684\u90ae\u7bb1\n    password = \"\"         #\u90ae\u7bb1\u7684\u6388\u6743\u7801\n    receiver = toemail  #\u63a5\u6536\u65b9\u7684\u90ae\u7bb1\n    data_time = time.strftime(\"%Y-%m-%d\")\n    subject = data_time + \"\u7684CSDN\u535a\u5ba2\u62a5\u544a\"   #title\u90ae\u4ef6\u6807\u9898\n    words = text   #text\u90ae\u4ef6\u5185\u5bb9\n    smtpserver = 'smtp.exmail.qq.com'\n    msg = MIMEText(words, 'plain', 'utf-8')#\u4e2d\u6587\u9700\u53c2\u6570\u2018utf-8'\uff0c\u5355\u5b57\u8282\u5b57\u7b26\u4e0d\u9700\u8981\n    msg['Subject'] = Header(subject, 'utf-8') #\u90ae\u4ef6\u6807\u9898\n    msg['from'] = sender #\u53d1\u4fe1\u4eba\u5730\u5740\n    msg['to'] = receiver #\u6536\u4fe1\u4eba\u5730\u5740\n    smtp = smtplib.SMTP_SSL(\"smtp.exmail.qq.com\", 465)\n    smtp.connect('smtp.exmail.qq.com')\n    smtp.login(sender, password)\n    smtp.sendmail(sender, receiver, msg.as_string())\n    smtp.quit()\n    print data_time + \"\u7684\u90ae\u4ef6\u53d1\u9001\u6210\u529f!\"\ndef operator_SQL(flag,update=None):\n    # \u5c06\u83b7\u53d6\u7684\u6570\u636e\u5b58\u5165\u6570\u636e\u5e93\u4e2d\n    try:\n        conn = MySQLdb.connect(\n            host='127.0.0.1',\n            port=3306,\n            user='root',\n            passwd='root',\n            db='test',\n            charset='utf8',\n        )\n    except:\n        conn = MySQLdb.connect(\n            host='127.0.0.1',\n            port=3306,\n            user='test',\n            passwd='',\n            db='test',\n            charset='utf8',\n        )\n    cur = conn.cursor()\n\n    if flag == 1:\n        res = cur.execute(\"select * from csdn_article\")\n        cur.execute(\n            'insert into csdn_article values(\"%s\",\"%s\", \"%s\", \"%s\", \"%s\")' % (res + 1, update[0], update[1], update[2], 0))\n    elif flag == 2:\n        cur.execute(\"update csdn_article set article_read=\"+str(update[1])+\" where article_id = \"+str(update[0]))\n        cur.execute(\"update csdn_article set change_read=\" + str(\n            update[3]) + \"  where article_id = \" + str(update[0]))\n    elif flag == 3:\n        res = cur.execute(\"select article_read from csdn_article where article_id=\"+str(update))\n        if res == 1:\n            res = cur.fetchmany(res)[0][0]\n        elif res == 0:\n            res = -1\n        return res\n        # sql_list = list(cur.fetchmany(res))\n        # print sql_list[1].count(435)\n        # if 435 in sql_list:\n        #     print '432432443242'\n    cur.close()\n    conn.commit()\n    conn.close()\ndef extract(html):\n    soup = BeautifulSoup(html)\n    # \u83b7\u53d6\u5230\u8bc4\u8bba\u7b49\u5185\u5bb9\n    comment = soup.find(attrs={'id':'blog_statistics'}).text\n    # \u63a5\u4e0b\u6765\u83b7\u53d6\u6bcf\u4e00\u7bc7\u6587\u7ae0\u7684\u9605\u8bfb\u6570\uff0c\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u6709\u53d8\u5316\uff0c\u5219\u8bb0\u5f55\u4e0b\u6765\n    article_soup = soup.find_all(attrs={'class':'list_item article_item'})\n\n    article_all_list = []\n    for value in article_soup:\n        # \u6839\u636e\u6bcf\u7bc7\u6587\u7ae0\u7684\u7279\u5f81\u63d0\u53d6\u5bf9\u5e94\u7684\u6570\u636e\n        value = str(value)\n        tmp_soup = BeautifulSoup(value)\n        article_id = tmp_soup.a['href']\n        article_title = tmp_soup.find('span',class_='link_title').contents[0].string\n        article_read = tmp_soup.find('span',class_='link_view').contents[1].string\n        # print article_id,article_read,article_title\n        # \u4f7f\u7528\u4e86\u5404\u79cd\u529e\u6cd5\uff0c\u7ec8\u4e8e\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u63d0\u53d6\u51fa\u6765\u4e86\n        article_id = int(str.split(str(article_id),'\/')[4])\n        article_read = int(str.split(str(article_read[1:]),')')[0])\n        # \u7136\u540e\u5c06\u6bcf\u4e00\u7bc7\u6587\u7ae0\u91cc\u7684\u6570\u636e\u653e\u5165\u4e00\u4e2a\u5217\u8868\u91cc\uff0c\u518d\u5c06\u8fd9\u4e2a\u5217\u8868\u653e\u5230\u5927\u7684\u5217\u8868\u91cc\n        article_list = []\n        article_list.append(article_id)\n        article_list.append(article_read)\n        article_list.append(article_title)\n        # \u5bf9\u6bcf\u4e2a\u503c\u53bb\u6570\u636e\u5e93\u91cc\u67e5\u627e\uff0c\u5982\u679c\u5b58\u5728\u5c31\u8fdb\u884c\u6bd4\u8f83\uff0c\u4e0d\u76f8\u540c\u5219\u8bb0\u5f55\u4e0b\u6765\uff0c\u5e76\u53bb\u6570\u636e\u5e93\u4e2d\u4fee\u6539\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u63d2\u5165\u8fd9\u6761\u6570\u636e\n\n        read = operator_SQL(3, article_id)\n        if read == -1:\n            operator_SQL(1, article_list)\n        elif read != article_read:\n            # \u5c06\u9605\u8bfb\u6b21\u6570\u7684\u67e5\u503c\u4f20\u5165\u6570\u636e\u5e93\u4e2d\n            article_list.append(article_read - read)\n            # print article_list[3]\n            operator_SQL(2, article_list)\n            article_all_list.append(article_list)\n    text = \"\u60a8\u4eca\u5929\u7684CSDN\u535a\u5ba2\u4fe1\u606f\u62a5\u544a\u5982\u4e0b\uff1a\"\n    text = text + str(comment)\n    text = text + '\u4eca\u65e5\u6709' + str(article_all_list.__len__()) + '\u7bc7\u6587\u7ae0\u7684\u9605\u8bfb\u6570\u53d1\u751f\u4e86\u53d8\u5316' + '\\n'\n    i = 1\n    for value in article_all_list:\n        # print value[0]\n        text = text + str(i) + '\uff1a ' + value[2]  + '\u603b\u9605\u8bfb\u6b21\u6570\u4e3a\uff1a'+ str(value[1]) + '\uff1b\u6bd4\u6628\u5929\u589e\u52a0\u4e86\uff1a' + str(value[3]) + '\u6b21\u3002' + '\\n'\n        i = i + 1\n\n    # print text\n    return text\ndef main():\n    url = 'http:\/\/blog.csdn.net\/rain_web\/article\/list\/500'\n    html = download(url)\n    text = extract(html=html)\n    email(text,'nylrain@163.com')\n    # operator_SQL(3, 353425)\nif __name__=='__main__':\n    main()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u52a8\u68c0\u6d4bCSDN\u535a\u5ba2\u6587\u7ae0\u9605\u8bfb\u6b21\u6570\u7684\u722c\u866b \u5e73\u65f6\u6ca1\u4e8b\u4f1a\u53bbCSDN\u4e0a\u770b\u4e00\u770b\u535a\u5ba2\u91cc\u6709\u4ec0\u4e48\u53d8\u5316\uff0c\u5982\u679c\u6587\u7ae0\u6709\u8bc4\u8bba\u6216\u8005\u6709\u4e86\u79c1\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/08\/03\/article91\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":837,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[58,78,81],"class_list":["post-836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-server","tag-reptile"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/836","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=836"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/836\/revisions"}],"predecessor-version":[{"id":1442,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/836\/revisions\/1442"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/837"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}