{"id":213,"date":"2022-09-28T09:42:23","date_gmt":"2022-09-28T01:42:23","guid":{"rendered":"https:\/\/h4ckm310n.com\/?p=213"},"modified":"2024-12-14T19:37:06","modified_gmt":"2024-12-14T11:37:06","slug":"%e5%ae%9e%e4%b9%a0%e6%9c%9f%e9%97%b4%e7%9a%84%e6%8a%80%e6%9c%af%e7%9b%b8%e5%85%b3%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/h4ckm310n.com\/?p=213","title":{"rendered":"\u5b9e\u4e60\u671f\u95f4\u7684\u6280\u672f\u76f8\u5173\u8bb0\u5f55"},"content":{"rendered":"<p>\u56fd\u5e86\u5feb\u5230\u4e86\uff0c\u6211\u4e3a\u671f\u4e00\u4e2a\u6708\u7684\u5b9e\u4e60\u4e5f\u5373\u5c06\u7ed3\u675f\uff0c\u8fd9\u4e24\u5929\u6253\u7b97\u56de\u987e\u4e00\u4e0b\u8fd9\u4e00\u4e2a\u6708\u4ee5\u6765\u5199\u8fc7\u7684\u4ee3\u7801\u5e76\u5bf9\u4e8e\u81ea\u5df1\u5728\u8fd9\u8fc7\u7a0b\u4e2d\u5b66\u5230\u7684\u4e00\u4e9b\u65b0\u59ff\u52bf\u505a\u4e00\u4e2a\u7b80\u8981\u7684\u8bb0\u5f55\uff0c\u5185\u5bb9\u53ef\u80fd\u5199\u7684\u6bd4\u8f83\u7a00\u788e\uff0c\u5c31\u5f53\u662f\u5907\u5fd8\u5f55\u4e86\u3002<br \/>\n<!--more--><\/p>\n<h4>1. \u89e3\u538bzip\uff08\u5e76\u5220\u9664\u539fzip\u6587\u4ef6\uff09<\/h4>\n<pre>with zipfile.ZipFile(file_path, 'r') as f:\r\n  f.extractall(curr_dir)\r\nos.remove(file_path)\r\n<\/pre>\n<h4>2. \u642d\u5efaFTP\u670d\u52a1\u5668<\/h4>\n<pre>from pyftpdlib.servers import FTPServer\r\nfrom pyftpdlib.handlers import FTPHandler\r\nfrom pyftpdlib.authorizers import DummyAuthorizer\r\n\r\nauth = DummyAuthorizer()\r\nauth.add_user('username', 'password', 'D:\/path', perm='elradfmw')\r\nhandler = FTPHandler\r\nhandler.authorizer = auth\r\nserver = FTPServer(('10.12.34.56', 1234), handler)\r\nserver.serve_forever()\r\n<\/pre>\n<h4>3. \u6d6e\u70b9\u6570\u76f8\u52a0\u95ee\u9898<\/h4>\n<p>\u6709\u65f6\u5019\u4e24\u4e2a\u6709\u9650\u5c0f\u6570\u76f8\u52a0\uff0c\u540e\u9762\u4f1a\u591a\u51fa\u5f88\u591a\u4e2a9\u62160\uff0c\u4f8b\u5982\uff1a<br \/>\n6.3 + 0.43 = 6.7299999999999995, 0.1 + 0.2 = 0.30000000000000004<br \/>\n\u867d\u7136\u6ca1\u641e\u61c2\u8fd9\u4e2a\u73b0\u8c61\u662f\u600e\u4e48\u56de\u4e8b\uff0c\u4f46\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u901a\u8fc7round\u6765\u5904\u7406\uff0c\u4f8b\u5982\u56db\u820d\u4e94\u5165\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff1a<\/p>\n<pre>num = round(num, 2)\r\n<\/pre>\n<h4>4. Pandas\u7684Dataframe\u6392\u5e8f<\/h4>\n<p>\u6307\u5b9a\u4e00\u4e2a\u5217\u540d\uff0c\u5e76\u6839\u636e\u8fd9\u4e00\u5217\u5bf9\u6570\u636e\u6392\u5e8f\uff1a<\/p>\n<pre>df = df.sort_values('col_1')\r\n<\/pre>\n<h4>5. Selenium\u83b7\u53d6\u5143\u7d20\u5185\u7684\u6587\u672c<\/h4>\n<p>\u901a\u5e38\uff0c\u83b7\u53d6\u6587\u672c\u53ef\u4ee5\u901a\u8fc7.text\u6765\u83b7\u53d6\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>a = driver.find_element(By.XPATH, '\/\/xpath').text\r\n<\/pre>\n<p>\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u5143\u7d20\uff0c\u8fd9\u4e48\u83b7\u53d6\u5f97\u5230\u7684\u5185\u5bb9\u5374\u662f\u7a7a\u7684\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u7528.get_attribute()\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\uff1a<\/p>\n<pre>a = driver.find_element(By.XPATH, '\/\/xpath').get_attribute('innerText')\r\n<\/pre>\n<h4>6. Selenium\u6a21\u62df\u5143\u7d20\u70b9\u51fb<\/h4>\n<p>\u5bf9\u4e8ebutton\u8fd9\u6837\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528click()\u6765\u70b9\u51fb\uff1a<\/p>\n<pre>driver.find_element(By.XPATH, '\/\/button[1]').click()\r\n<\/pre>\n<p>\u7136\u800c\uff0c\u6709\u4e9b\u5143\u7d20\u867d\u7136\u5728\u7f51\u9875\u4e2d\u4e5f\u53ef\u4ee5\u70b9\u51fb\uff0c\u4f46\u662f\u76f4\u63a5\u7528click()\u7684\u8bdd\u4f1a\u62a5\u9519\uff08\u4f8b\u5982span\uff09\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ea\u80fd\u8c03\u7528js\u811a\u672c\u6765\u5b8c\u6210\uff1a<\/p>\n<pre># &lt;li&gt;&lt;span&gt;&lt;\/span&gt;&lt;\/li&gt;\r\ndriver.execute_script('document.evaluate(\\'\/\/li[0]\\', document, null, XPathResult.ANY_TYPE, null).iterateNext().children[0].click();')\r\n<\/pre>\n<h4>7. Pandas\u76f4\u63a5\u8bfb\u53d6\u8bf7\u6c42\u7684\u8868\u683c\u5185\u5bb9<\/h4>\n<p>\u6709\u65f6\u5019\u9700\u8981\u5bf9requests\u8bf7\u6c42\u8fd4\u56de\u7684\u8868\u683c\u5185\u5bb9\u5148\u8fdb\u884c\u5904\u7406\u518d\u4fdd\u5b58\u5230\u786c\u76d8\u672c\u5730\uff0c\u4e00\u79cd\u65b9\u6cd5\u662f\u5148\u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u518d\u8bfb\u53d6\uff0c\u518d\u4fdd\u5b58\u3002\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528BytesIO\u3002<\/p>\n<pre>from io import BytesIO\r\n\r\nr = requests.get(url)\r\ndf = pd.read_csv(BytesIO(r.content))\r\n<\/pre>\n<h4>8. Pandas\u83b7\u53d6\u4e24\u4e2aDataframe\u7684\u5dee\u96c6<\/h4>\n<p>\u5982df1 &#8211; df2\uff1a<\/p>\n<pre>dff = pd.concat([df1, df2, df2]).drop_duplicates(keep=False)\r\n<\/pre>\n<h4>9. \u7ed3\u5408selenium\u4e0erequests<\/h4>\n<p>\u6709\u7684\u7f51\u7ad9\u5728\u767b\u5f55\u7684\u65f6\u5019\u4e0d\u4f1a\u76f4\u63a5\u53d1\u9001\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5bf9\u5bc6\u7801\u4f7f\u7528\u67d0\u79cd\u7b97\u6cd5\u52a0\u5bc6\u540e\u518d\u53d1\u9001\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u5206\u6790js\u4ee3\u7801\u6bd4\u8f83\u9ebb\u70e6\uff0c\u56fe\u7701\u4e8b\u53ef\u4ee5\u76f4\u63a5\u7528selenium\u767b\u5f55\u3002\u540e\u7eed\u7684\u8bf7\u6c42\u64cd\u4f5c\u4f7f\u7528requests\u6548\u7387\u66f4\u9ad8\uff0c\u6240\u4ee5\u53ef\u4ee5\u5148\u901a\u8fc7selenium\u767b\u5f55\u540e\u83b7\u53d6cookies\uff0c\u518d\u8ba9requests\u4f7f\u7528\u8be5cookies\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<pre>headers = {}\r\ndriver.get('xxx.com\/login')\r\ndriver.find_element(By.ID, 'username').send_keys('username')\r\ndriver.find_element(By.ID, 'password').send_keys('password')\r\ndriver.find_element(By.ID, 'btn').click()\r\ncookie = driver.get_cookie('sessionid')\r\nheaders['Cookie'] = '='.join([cookie['name'], cookie['value']])\r\nr = requests.get('xxx.com\/xxx', headers=headers)\r\n<\/pre>\n<h4>10. docx\/wps\u6587\u6863\u7684\u7f16\u8f91<\/h4>\n<p>\u6709\u65f6\u5019\u9700\u8981\u6279\u91cf\u4fee\u6539\u6587\u6863\uff0c\u628a\u5bf9\u5e94\u7684\u4f4d\u7f6e\u5206\u522b\u66ff\u6362\u4e3a\u4e0d\u540c\u6587\u672c\uff0c\u5982\u679c\u76f4\u63a5\u4fee\u6539paragraph\u7684text\u7684\u8bdd\uff0c\u4f1a\u9020\u6210\u683c\u5f0f\u4e22\u5931\uff0c\u6240\u4ee5\u9700\u8981\u4fee\u6539paragraph\u7684run\u91cc\u9762\u7684text\u3002<\/p>\n<pre>\r\nimport docx\r\ndocument = docx.Document('xxx.docx')\r\ndocument.paragraphs[1].runs[2].text = document.paragraphs[1].runs[2].text.replace('a', 'b')\r\ndocument.save('yyy.docx')\r\n<\/pre>\n<h4>11. Requests\u6587\u4ef6\u4e0a\u4f20<\/h4>\n<pre>\r\nfrom urllib3 import encode_multipart_formdata\r\n\r\ndata = {'file': ('filename.txt', open('filename.txt', 'rb').read())}\r\nencoded = encode_multipart_formdata(data)\r\ndata = encoded[0]\r\nheaders['Content-Type'] = encoded[1]\r\nr = requests.post('xxx.com\/xxx', data=data, headers=headers)\r\n<\/pre>\n<h4>12. Selenium\u83b7\u53d6session storage<\/h4>\n<p>\u6709\u4e9b\u65f6\u5019\u53d1\u8d77\u8bf7\u6c42\u8981\u4f7f\u7528token\u800c\u4e0d\u662fcookie\uff0c\u8fd9\u73a9\u610f\u513f\u53ef\u80fd\u4f1a\u653e\u5728\u6d4f\u89c8\u5668\u7684session storage\u4e2d\uff0c\u6709\u65f6\u53ef\u80fd\u65e0\u6cd5\u76f4\u63a5\u5728\u7a97\u53e3\u4e2d\u83b7\u53d6\u5230\uff0c\u9700\u8981\u5207\u6362\u5230\u67d0\u4e2aframe\u5f53\u4e2d\u624d\u80fd\u83b7\u53d6\uff0c\u53ef\u4ee5\u901a\u8fc7js\u6765\u83b7\u53d6\uff1a<\/p>\n<pre>\r\ndriver.switch_to.frame('framename')\r\ntoken = driver.execute_script('return sessionStorage[\"Token\"];')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56fd\u5e86\u5feb\u5230\u4e86\uff0c\u6211\u4e3a\u671f\u4e00\u4e2a\u6708\u7684\u5b9e\u4e60\u4e5f\u5373\u5c06\u7ed3\u675f\uff0c\u8fd9\u4e24\u5929\u6253\u7b97\u56de\u987e\u4e00\u4e0b\u8fd9\u4e00\u4e2a\u6708\u4ee5\u6765\u5199\u8fc7\u7684\u4ee3\u7801\u5e76\u5bf9\u4e8e\u81ea\u5df1\u5728\u8fd9\u8fc7\u7a0b\u4e2d\u5b66\u5230\u7684\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[24,6],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-24","category-6"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"h4ckm310n","author_link":"https:\/\/h4ckm310n.com\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":"\u56fd\u5e86\u5feb\u5230\u4e86\uff0c\u6211\u4e3a\u671f\u4e00\u4e2a\u6708\u7684\u5b9e\u4e60\u4e5f\u5373\u5c06\u7ed3\u675f\uff0c\u8fd9\u4e24\u5929\u6253\u7b97\u56de\u987e\u4e00\u4e0b\u8fd9\u4e00\u4e2a\u6708\u4ee5\u6765\u5199\u8fc7\u7684\u4ee3\u7801\u5e76\u5bf9\u4e8e\u81ea\u5df1\u5728\u8fd9\u8fc7\u7a0b\u4e2d\u5b66\u5230\u7684\u4e00&hellip;","_links":{"self":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":11,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/h4ckm310n.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}