{"id":38128,"date":"2025-05-19T14:14:17","date_gmt":"2025-05-19T06:14:17","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38128.html"},"modified":"2025-05-19T14:14:17","modified_gmt":"2025-05-19T06:14:17","slug":"python%e7%88%ac%e8%99%ab%e5%ae%9e%e6%88%98%e8%ae%ad%e7%bb%83","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38128.html","title":{"rendered":"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3"},"content":{"rendered":"<p>\u524d\u8a00&#xff1a;\u54c7&#xff0c;\u4eca\u5929\u7ec8\u4e8e\u80fd\u8bbf\u95ee\u8c46\u74e3\u4e86&#xff0c;\u524d\u51e0\u5929\u722c\u592a\u591a\u6b21\u4e86&#xff0c;\u7f51\u9875\u90fd\u4e0d\u8ba9\u6211\u8bbf\u95ee\u4e86&#xff08;\u8981\u767b\u5f55&#xff09;\u3002<\/p>\n<h2>\u5148\u6765\u4e2a\u5c0f\u7ec3\u4e60\u8bd5\u8bd5\u624b\u5427&#xff01;<\/h2>\n<p>\u722c\u53d6\u8c46\u74e3\u7b2c\u4e00\u9875&#xff08;\u591a\u9875\u540c\u4e0a\u7bc7\u6587\u7ae0&#xff09;\u6240\u6709\u7535\u5f71\u7684\u6392\u540d\u3001\u7535\u5f71\u540d\u79f0\u3001\u661f\u7ea7\u548c\u8bc4\u5206&#xff0c;\u5e76\u7528Excel\u5b58\u50a8<\/p>\n<p>\u7f51\u5740\u662f&#xff1a;\u8c46\u74e3\u7535\u5f71 Top 250\u00a0 \u5927\u5bb6\u5148\u81ea\u5df1\u5c1d\u8bd5\u4e00\u4e0b\u5427&#xff0c;\u8fd8\u662f\u7b80\u5355\u7684&#xff0c;\u6211\u5c31\u76f4\u63a5\u653e\u4ee3\u7801\u4e86<\/p>\n<p>import requests<br \/>\nfrom bs4 import BeautifulSoup<br \/>\nimport pandas as pd<br \/>\nheaders&#061;{&#034;User-Agent&#034;: &#034;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&#034;}<br \/>\nr&#061;requests.get(&#034;https:\/\/movie.douban.com\/top250&#034;,headers&#061;headers)<br \/>\nif r.status_code!&#061;200:<br \/>\n    raise Exception(&#034;error&#034;)<br \/>\nsoup&#061;BeautifulSoup(r.text,&#034;html.parser&#034;)<br \/>\ndatas&#061;[[&#039;\u6392\u540d&#039;,&#039;\u7535\u5f71\u540d\u79f0&#039;,&#039;\u661f\u7ea7&#039;,&#039;\u8bc4\u5206&#039;]]<br \/>\narticles&#061;soup.find_all(&#039;div&#039;,class_&#061;&#039;item&#039;)<br \/>\nfor article in articles:<br \/>\n    rank&#061;article.find(&#039;em&#039;).get_text()<br \/>\n    title&#061;article.find(&#039;span&#039;,class_&#061;&#039;title&#039;).get_text()    #\u661f\u7ea7\u5728class\u5c5e\u6027\u91cc,get(&#039;class&#039;)\u8fd4\u56de\u7684\u662f\u5217\u8868,\u56e0\u4e3aHTML\u7684class\u5c5e\u6027\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u7c7b\u540d,\u56e0\u6b64BeautifulSoup\u5c06\u5176\u5b58\u50a8\u4e3a\u4e00\u4e2a\u5217\u8868<br \/>\n    star&#061;article.find(&#039;div&#039;,class_&#061;&#039;bd&#039;).find(&#039;span&#039;).get(&#039;class&#039;)[0].replace(&#039;rating&#039;,&#039;&#039;).replace(&#039;-t&#039;,&#039;&#039;) #string\u7684replace\u65b9\u6cd5,\u53ea\u4fdd\u7559\u6570\u5b57<br \/>\n    score&#061;article.find(&#039;span&#039;,class_&#061;&#039;rating_num&#039;).get_text()<br \/>\n    datas.append([rank,title,star,score])<br \/>\ndf&#061;pd.DataFrame(datas)<br \/>\ndf.to_excel(&#039;doubanTop25.xlsx&#039;)<\/p>\n<p>easy\u5427&#xff0c;\u4e0d\u8fc7&#xff0c;\u503c\u5f97\u63d0\u4e00\u4e0b\u7684\u662f&#xff0c;\u7f51\u9875\u5e26\u5c0f\u6570\u661f\u7ea7\u7684\u8868\u793a\u4e0d\u51c6\u786e&#xff0c;4.5\u661f\u7ea7\u4e3a45<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061412-682acc34b42ee.png\" \/><\/p>\n<p>\u53ef\u4ee5\u89c2\u5bdf\u5230star\u4e0escore\u90fd\u5728\u540c\u4e00\u4e2adiv\u6807\u7b7e\u4e0b&#xff0c;\u6240\u4ee5\u8fd8\u53ef\u4ee5\u7528\u8fd9\u4e24\u884c\u4ee3\u7801\u4ee3\u66ff<\/p>\n<p>data&#061;article.find(&#039;div&#039;,class_&#061;&#039;bd&#039;).find(&#039;div&#039;).find_all(&#039;span&#039;)<br \/>\nstar,score&#061;data[0][&#039;class&#039;][0],data[1].get_text()      #\u8fd9\u6837star\u5c31\u662f\u4e00\u6574\u4e2a\u5b57\u7b26 <\/p>\n<h2>\u00a0\u722c\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875<\/h2>\n<p>\u6211\u4eec\u6765\u8bd5\u7740\u722c\u53d6\u676d\u5dde\u4eca\u5e745\u4e2a\u6708\u7684\u5929\u6c14\u6570\u636e&#xff08;\u6700\u8fd1\u90fd\u662f\u4e0b\u96e8&#xff0c;\u6709\u70b9\u4e0d\u559c\u6b22\u54e6&#xff0c;\u5c0f\u5c0f\u7684\u6bdb\u6bdb\u7ec6\u96e8\u6211\u89c9\u5f97\u8fd8\u597d&#xff0c;\u4f46\u662f\u5927\u66b4\u96e8\u771f\u662f\u4ec0\u4e48\u90fd\u4e0d\u65b9\u4fbf&#xff09;<\/p>\n<p>\u6211\u4eec\u9009\u62e9\u4e0d\u540c\u7684\u6708\u4efd&#xff0c;\u53ef\u4ee5\u53d1\u73b0\u7f51\u9875\u7684url\u90fd\u6ca1\u6709\u53d1\u751f\u6539\u53d8&#xff0c;\u8bf4\u660e\u8fd9\u4e2a\u7f51\u9875\u4e0d\u662f\u9759\u6001\u7f51\u9875&#xff0c;\u5b83\u662f\u540e\u53f0\u5f02\u6b65\u52a0\u8f7d\u7684\u52a8\u6001\u7f51\u9875&#xff0c;\u6211\u4eec\u8868\u9762\u4e0d\u80fd\u77e5\u9053\u5b83\u5b9e\u9645\u7684\u94fe\u63a5\u7684&#xff0c;\u90a3\u4e48\u6211\u4eec\u9700\u8981\u6293\u5305\u6765\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061412-682acc34dd13d.png\" \/><\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061413-682acc352e681.png\" \/><\/p>\n<p>\u53f3\u952e&#xff0c;\u70b9\u51fb\u201c\u68c0\u67e5\u201d&#xff0c;\u70b9\u51fb\u201c\u7f51\u7edc\u201d<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061413-682acc356c642.png\" \/><\/p>\n<p>\u4e0d\u8981\u76f4\u63a5\u70b9\u51fb\u201c\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u201d&#xff0c;\u90a3\u4f1a\u52a0\u8f7d\u5927\u91cf\u9875\u9762\u5f39\u51fa\u6765\u4e00\u5927\u5806\u3002\u6211\u4eec\u518d\u6b21\u67e5\u8be2\u4e00\u4e2a\u6708\u4efd\u4fe1\u606f&#xff0c;\u53d1\u73b0\u540e\u53f0\u4f1a\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42\u201cGetHistoty\u2026\u2026\u201d&#xff08;\u6216\u8005\u70b9\u51fbFecth\/XHR\u8fd9\u662f\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\u7684\u610f\u601d&#xff0c;\u91cc\u9762\u5c31\u662f\u6211\u4eec\u8981\u6293\u7684\u5305&#xff09;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061413-682acc359a441.png\" \/>\u00a0\u70b9\u51fb\u201c\u8bf7\u6c42\u201d\u8fdb\u53bb\u770b\u4e00\u4e0b&#xff0c;\u53ef\u4ee5\u53d1\u73b0\u201c\u8bf7\u6c42URL\u201d\u4e0e\u7f51\u9875\u4e0a\u65b9\u7684url\u4e0d\u662f\u540c\u4e00\u4e2a&#xff0c;\u8fd9\u4e2a\u5c31\u662f\u5f02\u6b65\u52a0\u8f7d\u7684&#xff0c;\u8bf7\u6c42\u65b9\u5f0f\u4e3a\u201cGET\u201d\u3002\u590d\u5236URL\u201c&#xff1f;\u201d\u524d\u9762\u7684\u90e8\u5206&#xff08;\u540e\u9762\u662f\u53c2\u6570\u90e8\u5206&#xff0c;\u90fd\u5728\u8d1f\u8f7d\u91cc&#xff09;\u4e3aurl<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061413-682acc35cf2cc.png\" \/><\/p>\n<p>\u590d\u5236\u201c\u8bf7\u6c42\u6807\u5934\u201d\u91cc\u7684User-Agent&#xff0c;\u8bbe\u7f6e\u4e3aheaders\u7528\u4e8e\u53cd\u722c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc360f12d.png\" \/><\/p>\n<p>\u8fd9\u4e2a\u7f51\u7ad9\u7684\u53cd\u722c\u505a\u7684\u6709\u70b9\u597d\u554a&#xff0c;\u8fd8\u9700\u8981\u8bbe\u7f6eheaders\u7684Referer\u5c5e\u6027 &#xff0c;\u4e5f\u5728\u8bf7\u6c42\u6807\u5934\u91cc\u9762&#xff0c;\u590d\u5236\u4e0b\u6765<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc3621455.png\" \/><\/p>\n<p>\u00a0\u70b9\u51fb\u201c\u8d1f\u8f7d\u201d&#xff0c;\u524d\u4e24\u9879\u662f\u676d\u5dde\u5730\u533a\u6709\u5173\u7684\u7f16\u7801&#xff0c;\u90fd\u662f\u4e0d\u4f1a\u53d8\u7684&#xff0c;\u4e0b\u9762\u4e24\u9879\u5c31\u662f\u67e5\u8be2\u7684year\u4e0emonth\u3002\u590d\u5236\u91cc\u9762\u7684\u5185\u5bb9\u4e3a\u8bf7\u6c42\u7684\u53c2\u6570&#xff08;\u8bbe\u4e3a\u5b57\u5178\u7c7b\u578b&#xff09;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc3635507.png\" \/><\/p>\n<p>\u70b9\u51fb\u201c\u54cd\u5e94\u201d&#xff0c;\u770b\u4e00\u4e0b\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u600e\u6837\u7684<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u54cd\u5e94\u662fjson\u7c7b\u578b\u7684\u6570\u636e&#xff08;JSON &#xff08;JavaScript Object Notation&#xff09;\u6570\u636e\u7531\u952e\u503c\u5bf9\u7ec4\u6210&#xff0c;\u7c7b\u4f3c\u4e8e\u5b57\u5178&#xff0c;\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f&#xff09;\u00a0<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc36565bd.png\" \/><\/p>\n<p>\u70b9\u51fb\u201c\u9884\u89c8\u201d&#xff0c;\u53ef\u4ee5\u770b\u5230\u683c\u5f0f\u5316\u7684\u5c55\u793a&#xff0c;\u5c06\u9f20\u6807\u79fb\u5230data\u7684\u503c\u53ef\u4ee5\u770b\u5230html\u7684\u6570\u636e&#xff08;\u622a\u56fe\u6ca1\u6cd5\u5c55\u793a&#xff0c;\u81ea\u884c\u770b&#xff09;&#xff0c;\u53ef\u4ee5\u53d1\u73b0\u91cc\u9762\u6709\u4e2a&lt;table&gt;\u6807\u7b7e\u00a0<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc367de9f.png\" \/><\/p>\n<p>\u201c\u54cd\u5e94\u201d\u91cc\u4e5f\u80fd\u770b\u5230&#xff0c;\u4e0d\u8fc7\u201c\u9884\u89c8\u201d\u53ef\u89c6\u5316\u66f4\u597d\u00a0<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc36a531a.png\" \/><\/p>\n<p>\u8868\u683c\u6570\u636e\u7528pandas\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u89e3\u6790\u00a0<\/p>\n<p>import requests<br \/>\nimport pandas as pd<br \/>\nfrom io import StringIO<br \/>\nurl&#061;&#039;https:\/\/tianqi.2345.com\/Pc\/GetHistory&#039;<br \/>\nheaders&#061;{&#034;User-Agent&#034;:&#039;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/136.0.0.0 Safari\/537.36 Edg\/136.0.0.0&#039;,&#039;Referer&#039;:&#039;https:\/\/tianqi.2345.com\/wea_history\/58457.htm&#039;}<br \/>\nparams&#061;{<br \/>\n&#034;areaInfo[areaId]&#034;:58457,<br \/>\n&#034;areaInfo[areaType]&#034;:2,<br \/>\n&#034;date[year]&#034;:2025,<br \/>\n&#034;date[month]&#034;:4<br \/>\n}<br \/>\nr&#061;requests.get(url,headers&#061;headers,params&#061;params)     #\u8bf7\u6c42\u5934\u548c\u53c2\u6570\u90fd\u4e3a\u5b57\u5178\u7c7b\u578b<br \/>\nif r.status_code!&#061;200:<br \/>\n    raise Exception(&#039;error&#039;)<br \/>\ndata&#061;r.json()[&#034;data&#034;]         #r.json()\u65b9\u6cd5\u4f1a\u5c06\u8fd4\u56de\u7684JSON\u683c\u5f0f\u7684\u54cd\u5e94\u89e3\u6790\u4e3a\u4e00\u4e2aPython\u5bf9\u8c61(\u4e00\u822c\u4e3a\u5b57\u5178\/\u5217\u8868),\u6211\u4eec\u53d6\u51fa\u2018data\u2019\u952e\u7684\u503c(\u662f\u5b57\u7b26\u4e32)<br \/>\ndata&#061;StringIO(data)           #\u4f7f\u7528StringIO\u5bf9\u8c61\u6765\u5305\u88c5HTML\u5b57\u7b26\u4e32&#xff0c;\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u89c6\u4e3a\u6587\u4ef6\u6765\u8bfb\u53d6<br \/>\ndf&#061;pd.read_html(data)[0]      #pd.read_html()\u65b9\u6cd5\u53ef\u4ee5\u89e3\u6790\u4e00\u4e2a\u7f51\u9875\u4e2d\u6240\u6709\u7684\u8868\u683c,\u8fd4\u56de\u4e00\u4e2a\u5217\u8868,\u91cc\u9762\u7684\u5143\u7d20\u662fDataFrame\u7684\u6570\u636e\u7ed3\u6784<br \/>\nprint(df) <\/p>\n<p>\u8fd9\u6837\u6211\u4eec\u5355\u4e2a\u7684\u7f51\u9875\u5c31\u722c\u53d6\u6210\u529f\u4e86&#xff0c;\u8f93\u51fa\u5982\u4e0b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc36d58a7.png\" \/><\/p>\n<p>\u00a0\u6211\u4eec\u73b0\u5728\u6765\u722c\u53d61-5\u6708\u7684\u6570\u636e&#xff0c;\u6839\u636e\u524d\u9762\u7684\u5206\u6790&#xff0c;\u53ea\u9700\u8981\u5c06\u53c2\u6570\u6539\u4e00\u4e0b\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p>import requests<br \/>\nimport pandas as pd<br \/>\nfrom io import StringIO<br \/>\nurl&#061;&#039;https:\/\/tianqi.2345.com\/Pc\/GetHistory&#039;<br \/>\nheaders&#061;{&#034;User-Agent&#034;:&#039;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/136.0.0.0 Safari\/537.36 Edg\/136.0.0.0&#039;,&#039;Referer&#039;:&#039;https:\/\/tianqi.2345.com\/wea_history\/58457.htm&#039;}<br \/>\ndef craw_weather(month):<br \/>\n    params&#061;{<br \/>\n        &#034;areaInfo[areaId]&#034;:58457,<br \/>\n        &#034;areaInfo[areaType]&#034;:2,<br \/>\n        &#034;date[year]&#034;:2025,<br \/>\n        &#034;date[month]&#034;:month<br \/>\n    }<br \/>\n    r&#061;requests.get(url,headers&#061;headers,params&#061;params)<br \/>\n    if r.status_code!&#061;200:<br \/>\n        raise Exception(&#039;error&#039;)<br \/>\n    data&#061;r.json()[&#034;data&#034;]<br \/>\n    data&#061;StringIO(data)<br \/>\n    df&#061;pd.read_html(data)[0]<br \/>\n    return df<br \/>\nlst&#061;[]                #\u91cc\u9762\u662f\u6bcf\u4e2a\u6708\u7684df\u6570\u636e<br \/>\nfor n in range(1,6):<br \/>\n    df&#061;craw_weather(n)<br \/>\n    lst.append(df)<br \/>\ndatas&#061;pd.concat(lst)        #pd.concat()\u65b9\u6cd5\u7528\u4e8e\u5c06\u591a\u4e2aPandas\u5bf9\u8c61(DataFrame\u6216Series)\u6cbf\u7740\u7279\u5b9a\u8f74\u8fde\u63a5\u8d77\u6765,\u975e\u5e38\u7075\u6d3b,\u53ef\u4ee5\u7528\u4e8e\u884c\u8fde\u63a5\u3001\u5217\u8fde\u63a5\u7b49\u591a\u79cd\u64cd\u4f5c<br \/>\ndatas.to_excel(&#039;\u676d\u5dde1-5\u6708\u5929\u6c14\u6570\u636e.xlsx&#039;,index&#061;False) #Pandas\u5728\u5c06df\u4fdd\u5b58\u4e3aExcel\u65f6,\u4f1a\u5c06df\u7684\u7d22\u5f15\u4f5c\u4e3a\u5355\u72ec\u7684\u4e00\u5217\u5199\u5165\u6587\u4ef6,\u8bbe\u7f6eindex&#061;False\u53ef\u4ee5\u4e0d\u5305\u542b\u7d22\u5f15\u5217<\/p>\n<p>\u5c55\u793a\u5982\u4e0b&#xff0c;\u6628\u5929\u7684\u6570\u636e\u90fd\u6709\u4e86<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061414-682acc36edb63.png\" \/><\/p>\n<h2>\u6279\u91cf\u722c\u53d6\u6b63\u672c\u5c0f\u8bf4<\/h2>\n<p>\u6574\u672c\u5c0f\u8bf4\u7a0d\u5fae\u6709\u70b9\u591a\u4e86&#xff0c;\u6211\u4eec\u5c31\u62ff\u756a\u8304\u5c0f\u8bf4\u7684top1\u4e3a\u4f8b\u5427&#xff0c;\u722c\u53d6\u524d10\u7ae0\u7684\u5185\u5bb9&#xff0c;\u5148\u6279\u91cf\u722c\u53d6\u6bcf\u4e00\u7ae0\u7684\u94fe\u63a5\u548c\u7ae0\u8282\u540d\u79f0&#xff0c;\u518d\u6839\u636e\u94fe\u63a5\u722c\u53d6\u6b63\u6587&#xff0c;\u6700\u540e\u5c06\u6587\u7ae0\u5199\u5230\u6587\u4ef6\u4e2d\u53bb<\/p>\n<p>\u7f51\u9875\u5730\u5740\u4e3a\u5341\u65e5\u7ec8\u7109\u5b8c\u6574\u7248\u5728\u7ebf\u514d\u8d39\u9605\u8bfb_\u5341\u65e5\u7ec8\u7109\u5c0f\u8bf4_\u756a\u8304\u5c0f\u8bf4\u5b98\u7f51<\/p>\n<p>\u76f4\u63a5\u68c0\u7d22\u5143\u7d20\u54c8&#xff0c;\u53ef\u4ee5\u53d1\u73b0\u6240\u6709\u7ae0\u8282\u90fd\u5728\u7279\u5b9a\u7684&lt;div&gt;\u6807\u7b7e\u4e0b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061415-682acc372bdc1.png\" \/><\/p>\n<p>\u6587\u7ae0\u5185\u5bb9\u4e5f\u5728\u7279\u5b9a&lt;div&gt;\u6807\u7b7e\u4e0b<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061415-682acc379b348.png\" \/>\u4ee3\u7801\u5982\u4e0b<\/p>\n<p>import requests<br \/>\nfrom bs4 import BeautifulSoup<br \/>\ndef get_urls():<br \/>\n    root_url&#061;&#039;https:\/\/fanqienovel.com\/page\/7143038691944959011&#039;<br \/>\n    r&#061;requests.get(root_url)<br \/>\n    if r.status_code!&#061;200:<br \/>\n        raise Exception(&#039;error&#039;)<br \/>\n    soup&#061;BeautifulSoup(r.text,&#034;html.parser&#034;)<br \/>\n    datas&#061;[]<br \/>\n    n&#061;0<br \/>\n    for chapter in soup.find(&#039;div&#039;,class_&#061;&#039;chapter&#039;).find_all(&#039;a&#039;):<br \/>\n        if n&#061;&#061;10:<br \/>\n            break<br \/>\n        datas.append([&#039;https:\/\/fanqienovel.com&#039;&#043;chapter[&#039;href&#039;],chapter.get_text()])<br \/>\n        n&#043;&#061;1<br \/>\n    return datas<br \/>\ndef get_chapter(url):<br \/>\n    r&#061;requests.get(url)<br \/>\n    if r.status_code!&#061;200:<br \/>\n        raise Exception(&#039;error&#039;)<br \/>\n    soup&#061;BeautifulSoup(r.text,&#034;html.parser&#034;)<br \/>\n    content&#061;soup.find(&#039;div&#039;,&#034;muye-reader-content noselect&#034;).get_text()<br \/>\n    return content<br \/>\nfor urls in get_urls():<br \/>\n    url,title&#061;urls<br \/>\n    with open(f&#039;{title}.txt&#039;,&#039;w&#039;,encoding&#061;&#039;utf-8&#039;) as ch:      #\u5199\u5165\u6587\u4ef6<br \/>\n        ch.write(get_chapter(url)) <\/p>\n<p>\u5c55\u793a\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061416-682acc3831c28.png\" \/><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061416-682acc3850d28.png\" \/><\/p>\n<p>\u6700\u540e\u6587\u7ae0\u5b57\u7b26\u662f\u4e71\u7801&#xff0c;\u56e0\u4e3a\u7f51\u9875\u6e90\u4ee3\u7801\u5c31\u662f\u4e71\u7801\u7684&#xff0c;\u67e5\u770b&lt;head&gt;\u6807\u7b7e\u91cc\u7684charset&#xff0c;\u7f16\u7801\u5c31\u662f\u2018utf-8\u2019\u6bcf\u9519&#xff0c;\u4e3b\u5305\u4e5f\u5c1d\u8bd5\u4e86\u51e0\u79cd\u65b9\u6cd5\u8fd8\u662f\u4e0d\u80fd\u6b63\u5e38\u663e\u793a&#xff0c;\u53ef\u80fd\u6d4f\u89c8\u5668\u53ef\u80fd\u4f1a\u5bf9\u663e\u793a\u7684\u5185\u5bb9\u8fdb\u884c\u4e00\u4e9b\u6e32\u67d3\u6216\u8f6c\u6362&#xff0c;\u6211\u4eec\u8bb2\u5230\u540e\u9762\u518d\u6765\u5c1d\u8bd5<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061416-682acc38893f2.png\" \/>\u90a3\u722c\u8fd9\u4e2a\u6709\u4ec0\u4e48\u7528\u5462&#xff1f;\u00a0\u6709\u4e9b\u76d7\u7248\u7f51\u7ad9\u4e0d\u662f\u5c0f\u5e7f\u544a\u5f88\u591a\u561b&#xff0c;\u90a3\u4f60\u5c31\u53ef\u4ee5\u628a\u6587\u7ae0\u722c\u4e0b\u6765\u770b\u54af~<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf45\u6b21\u3002\u54c7\uff0c\u4eca\u5929\u7ec8\u4e8e\u80fd\u8bbf\u95ee\u8c46\u74e3\u4e86\uff0c\u524d\u51e0\u5929\u722c\u592a\u591a\u6b21\u4e86\uff0c\u7f51\u9875\u90fd\u4e0d\u8ba9\u6211\u8bbf\u95ee\u4e86\uff08\u8981\u767b\u5f55\uff09\u3002<\/p>\n","protected":false},"author":2,"featured_media":38109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,677],"topic":[],"class_list":["post-38128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-python","tag-677"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38128.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf45\u6b21\u3002\u54c7\uff0c\u4eca\u5929\u7ec8\u4e8e\u80fd\u8bbf\u95ee\u8c46\u74e3\u4e86\uff0c\u524d\u51e0\u5929\u722c\u592a\u591a\u6b21\u4e86\uff0c\u7f51\u9875\u90fd\u4e0d\u8ba9\u6211\u8bbf\u95ee\u4e86\uff08\u8981\u767b\u5f55\uff09\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38128.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T06:14:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061412-682acc34b42ee.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38128.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38128.html\",\"name\":\"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-19T06:14:17+00:00\",\"dateModified\":\"2025-05-19T06:14:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38128.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38128.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38128.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38128.html","og_locale":"zh_CN","og_type":"article","og_title":"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf45\u6b21\u3002\u54c7\uff0c\u4eca\u5929\u7ec8\u4e8e\u80fd\u8bbf\u95ee\u8c46\u74e3\u4e86\uff0c\u524d\u51e0\u5929\u722c\u592a\u591a\u6b21\u4e86\uff0c\u7f51\u9875\u90fd\u4e0d\u8ba9\u6211\u8bbf\u95ee\u4e86\uff08\u8981\u767b\u5f55\uff09\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/38128.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-19T06:14:17+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250519061412-682acc34b42ee.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38128.html","url":"https:\/\/www.wsisp.com\/helps\/38128.html","name":"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-19T06:14:17+00:00","dateModified":"2025-05-19T06:14:17+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38128.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38128.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38128.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"python\u722c\u866b\u5b9e\u6218\u8bad\u7ec3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38128"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38128\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/38109"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38128"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}