﻿{"id":1280,"date":"2022-10-12T21:20:38","date_gmt":"2022-10-12T13:20:38","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=1280"},"modified":"2022-10-12T21:21:59","modified_gmt":"2022-10-12T13:21:59","slug":"%e7%94%a8python%e7%88%ac%e7%bd%91%e7%bb%9c%e5%b0%8f%e8%af%b4%e6%ba%90%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=1280","title":{"rendered":"\u7528PYTHON\u722c\u7f51\u7edc\u5c0f\u8bf4\u6e90\u4ee3\u7801"},"content":{"rendered":"<p>\u8be5\u4ee3\u7801\u57fa\u672c\u4e0a\u53ef\u4e0b\u8f7d\u7edd\u5927\u90e8\u5206\u5c0f\u8bf4\u7f51\u7ad9\u7684\u5c0f\u8bf4\uff0c\u6709\u95ee\u9898\u4e5f\u53ea\u9700\u8981\u8c03\u6574\u5c0f\u90e8\u5206\u7684\u4ee3\u7801\u3002<br \/>\n1\u3001\u4fee\u6539\u4e00\u4e0burl\u53d8\u91cf\uff0c\u6539\u4e3a\u5c0f\u8bf4\u76ee\u5f55\u5217\u8868\u9875\u9762\u3002<br \/>\n2\u3001\u4fee\u6539\u7b2c34\u884c\u201cfor i in range(20, len(tag_h3)):\u201d20\u8fd9\u4e2a\u503c\uff0c\u8ddf\u8e2a\u8fdb\u53bb\u53ef\u8c03\u6574\u4ece\u7b2c\u51e0\u7ae0\u5f00\u59cb\u4e0b\u8f7d\u3002<br \/>\n3\u3001\u7b2c36\u884c\u4e5f\u5f97\u6539\u6210\u7f51\u7ad9\u9996\u5730\u5740\u3002<br \/>\n<!--more--><\/p>\n<pre class=\"lang:python decode:true\"># -*- coding: utf-8 -*-\r\n\"\"\"\r\nSpyder Editor\r\n\r\nThis is a temporary script file.\r\n\"\"\"\r\n\r\nfrom bs4 import BeautifulSoup\r\nimport requests\r\nimport time\r\nimport os\r\nimport threading\r\n\r\nheaders = {'User-Agent':'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/94.0.4606.61 Safari\/537.36'}\r\nsaveDir = 'd:\/\u9e3f\u5929\u795e\u5c0a.txt'  \r\nurl = 'https:\/\/www.tycqzw.com\/62_62436\/'   \r\n\r\n\r\n# \u83b7\u5f97\u6240\u6709\u7ae0\u8282\u7684URL\r\ndef get_topic(url):\r\n    headers = {'User-Agent':'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/94.0.4606.61 Safari\/537.36'}\r\n    \r\n    \r\n\r\n    response = requests.get(url, headers=headers)\r\n    response.encoding = response.apparent_encoding\r\n\r\n    soup = BeautifulSoup(response.content, 'html.parser')\r\n    tag_h3 = soup.find_all('dd')\r\n    \r\n    f = open(saveDir, mode='a', encoding='utf8')\r\n    \r\n    try:\r\n        for i in range(520, len(tag_h3)):\r\n            topic_url = tag_h3[i].find('a').get('href')  # \u83b7\u5f97\u4e3b\u9898\u94fe\u63a5\r\n            topic_url = 'https:\/\/www.tycqzw.com\/'+topic_url   # \u4e3b\u9898\u9875\u9762 url\r\n            txt = tag_h3[i].find('a').get_text() # \u4e3b\u9898\u540d\u79f0\r\n            print(topic_url +' ' + txt)\r\n            f.write(get_txt(topic_url, txt))\r\n    finally:\r\n        f.close()\r\n        \r\n\r\n# \u4e0b\u8f7d\u94fe\u63a5\u9875\u9762\u4e2d\u7684\u6587\u672c\u5185\u5bb9\r\ndef get_txt(url, txt):\r\n    response = requests.get(url, headers=headers)\r\n    response.encoding = response.apparent_encoding\r\n    soup = BeautifulSoup(response.content, 'html.parser')\r\n    tag_txt = soup.find('div', id='content').get_text() #(separator='\\n')\r\n    i = tag_txt.find('(\u672c\u7ae0\u5b8c)')\r\n    return txt+'\\n'+tag_txt[:i]\r\n \r\nif __name__ == '__main__':\r\n    print('===== \u4e3b\u7a0b\u5e8f\u5f00\u59cb =====')\r\n    start = time.perf_counter()\r\n    get_topic(url)\r\n    tt = time.perf_counter() - start\r\n    print('===== \u4e3b\u7a0b\u5e8f\u7ed3\u675f\uff0c\u7528\u65f6 {:.2f}s ====='.format(tt))\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8be5\u4ee3\u7801\u57fa\u672c\u4e0a\u53ef\u4e0b\u8f7d\u7edd\u5927\u90e8\u5206\u5c0f\u8bf4\u7f51\u7ad9\u7684\u5c0f\u8bf4\uff0c\u6709\u95ee\u9898\u4e5f\u53ea\u9700\u8981\u8c03\u6574\u5c0f\u90e8\u5206\u7684\u4ee3\u7801\u3002 1\u3001\u4fee\u6539\u4e00\u4e0burl\u53d8\u91cf\uff0c\u6539\u4e3a\u5c0f\u8bf4\u76ee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[70,80],"class_list":["post-1280","post","type-post","status-publish","format-standard","hentry","category-program","tag-python","tag-80"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1280"}],"version-history":[{"count":2,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1282,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1280\/revisions\/1282"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}