{"id":2745,"date":"2023-09-18T16:42:57","date_gmt":"2023-09-18T08:42:57","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2745"},"modified":"2023-09-18T16:42:57","modified_gmt":"2023-09-18T08:42:57","slug":"2745","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2745.html","title":{"rendered":"python \u7f51\u7ad9\u9632\u7be1\u6539"},"content":{"rendered":"

Python\u662f\u4e00\u95e8\u975e\u5e38\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u7f51\u7ad9\u3002\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\uff0c\u9632\u6b62\u7f51\u7ad9\u88ab\u7be1\u6539\u662f\u975e\u5e38\u91cd\u8981\u7684\u5de5\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u5b9e\u73b0\u7f51\u7ad9\u9632\u7be1\u6539\u3002<\/p>\n

Python\u63d0\u4f9b\u4e86\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7f51\u7ad9\u9632\u7be1\u6539\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fBeautiful Soup\u5e93\u548cRequests\u5e93\u3002<\/p>\n

import requests\r\nfrom bs4 import BeautifulSoup\r\n# \u83b7\u53d6\u7f51\u7ad9\u5185\u5bb9\r\nurl = 'http:\/\/example.com\/index.html'\r\nresponse = requests.get(url)\r\nhtml = response.text\r\n# \u89e3\u6790\u7f51\u7ad9\u5185\u5bb9\r\nbsObj = BeautifulSoup(html, 'html.parser')<\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u4f7f\u7528Requests\u5e93\u5411\u6307\u5b9a\u7684\u7f51\u7ad9\u8bf7\u6c42\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528Beautiful Soup\u5e93\u5bf9\u54cd\u5e94\u6570\u636e\u8fdb\u884c\u89e3\u6790\u3002\u89e3\u6790\u51fa\u6765\u7684bsObj\u5bf9\u8c61\u5305\u542b\u4e86\u6574\u4e2a\u7f51\u9875\u7684\u4fe1\u606f\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7f51\u7ad9\u5185\u5bb9\u8fdb\u884c\u6821\u9a8c\uff0c\u4ee5\u5224\u65ad\u662f\u5426\u88ab\u7be1\u6539\u3002\u5e38\u7528\u7684\u6821\u9a8c\u65b9\u5f0f\u6709\u5982\u4e0b\u51e0\u79cd\uff1a<\/p>\n

1. MD5\u6821\u9a8c<\/p>\n

MD5\u662f\u4e00\u79cd\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u4e00\u4e2a128\u4f4d\u7684\u6570\u5b57\u6307\u7eb9\u3002\u56e0\u6b64\uff0cMD5\u88ab\u5e7f\u6cdb\u7528\u6765\u6821\u9a8c\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n

import hashlib\r\n# \u8ba1\u7b97\u7f51\u7ad9\u5185\u5bb9\u7684MD5\u503c\r\nmd5 = hashlib.md5(bsObj.encode('utf-8')).hexdigest()\r\n# \u8bfb\u53d6\u4e4b\u524d\u4fdd\u5b58\u7684MD5\u503c\r\nwith open('md5.txt', 'r') as f:\r\nold_md5 = f.read()\r\n# \u6bd4\u8f83\u4e24\u4e2aMD5\u503c\u662f\u5426\u76f8\u7b49\r\nif md5 == old_md5:\r\nprint('\u7f51\u7ad9\u5185\u5bb9\u6ca1\u6709\u88ab\u7be1\u6539')\r\nelse:\r\nprint('\u7f51\u7ad9\u5185\u5bb9\u53ef\u80fd\u88ab\u7be1\u6539')<\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u8ba1\u7b97\u4e86\u7f51\u7ad9\u5185\u5bb9\u7684MD5\u503c\uff0c\u5e76\u4e0e\u4e4b\u524d\u4fdd\u5b58\u7684MD5\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u4e2a\u503c\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u7f51\u7ad9\u5185\u5bb9\u6ca1\u6709\u88ab\u7be1\u6539\u3002<\/p>\n

2. \u65f6\u95f4\u6233\u6821\u9a8c<\/p>\n

\u65f6\u95f4\u6233\u662f\u6307\u683c\u6797\u5a01\u6cbb\u65f6\u95f41970\u5e7401\u670801\u65e500\u65f600\u520600\u79d2\u5230\u73b0\u5728\u7684\u603b\u79d2\u6570\u3002\u901a\u8fc7\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u6233\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u6709\u65b0\u7684\u5185\u5bb9\u6216\u8005\u6709\u5185\u5bb9\u88ab\u5220\u9664\u3002<\/p>\n

import time\r\n# \u8bb0\u5f55\u5f53\u524d\u65f6\u95f4\u6233\r\ntimestamp = int(time.time())\r\n# \u8bfb\u53d6\u4e4b\u524d\u4fdd\u5b58\u7684\u65f6\u95f4\u6233\r\nwith open('timestamp.txt', 'r') as f:\r\nold_timestamp = int(f.read())\r\n# \u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u6233\u7684\u5927\u5c0f\r\nif timestamp >old_timestamp:\r\nprint('\u7f51\u7ad9\u5185\u5bb9\u53ef\u80fd\u88ab\u7be1\u6539')\r\nelse:\r\nprint('\u7f51\u7ad9\u5185\u5bb9\u6ca1\u6709\u88ab\u7be1\u6539')<\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u8bb0\u5f55\u4e86\u5f53\u524d\u65f6\u95f4\u6233\uff0c\u5e76\u4e0e\u4e4b\u524d\u4fdd\u5b58\u7684\u65f6\u95f4\u6233\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u5f53\u524d\u65f6\u95f4\u6233\u6bd4\u4e4b\u524d\u7684\u65f6\u95f4\u6233\u66f4\u5927\uff0c\u5219\u8bf4\u660e\u7f51\u7ad9\u5185\u5bb9\u53ef\u80fd\u88ab\u7be1\u6539\u3002<\/p>\n

\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7f51\u7ad9\u9632\u7be1\u6539\u7684\u529f\u80fd\uff0c\u4fdd\u969c\u7f51\u7ad9\u5185\u5bb9\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u3002<\/p>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

Python\u662f\u4e00\u95e8\u975e\u5e38\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u7f51\u7ad9\u3002\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\uff0c\u9632\u6b62\u7f51\u7ad9\u88ab\u7be1\u6539\u662f\u975e\u5e38\u91cd\u8981\u7684\u5de5\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u5b9e\u73b0\u7f51\u7ad9\u9632\u7be1\u6539\u3002<\/p>\n

Python\u63d0\u4f9b\u4e86\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7f51\u7ad9\u9632\u7be1\u6539\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fBeautiful Soup\u5e93\u548cRequests\u5e93\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2745","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2745"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2745"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2745\/revisions"}],"predecessor-version":[{"id":2746,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2745\/revisions\/2746"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}