{"id":234954,"date":"2022-02-05T09:49:16","date_gmt":"2022-02-05T01:49:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=234954"},"modified":"2022-01-29T15:49:41","modified_gmt":"2022-01-29T07:49:41","slug":"python-selenium-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-selenium-linux.html","title":{"rendered":"\u6559\u4f60\u4e24\u79cdpython selenium\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u5bb6\u597d\uff0c\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u7684\u662fpython selenium\u4fdd\u5b58\u56fe\u7247\u6700\u597d\u7684\u4e24\u79cd\u65b9\u6cd5\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u8d76\u5feb\u6765\u770b\u4e00\u770b\u5427\uff0c\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\u8bb0\u5f97\u6536\u85cf\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

selenium\u722c\u53d6\u9875\u9762\u65f6\u7ecf\u5e38\u9047\u5230\u8981\u4fdd\u5b58\u56fe\u7247\u7684\u9700\u6c42\uff0c\u901a\u5e38\u7684\u505a\u6cd5\u662f\u83b7\u53d6\u94fe\u63a5\u540e\u7528 requests \u4e0b\u8f7d\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u8131\u79bb\u4e86selenium\u73af\u5883\uff0c\u5982\u9047\u5230\u6709\u6821\u9a8c\u7684\u60c5\u51b5\u8fd8\u9700\u8981\u7ed5\u8fc7\u6821\u9a8c\u3002<\/p>\n

\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u76f4\u63a5\u901a\u8fc7selenium\u4fdd\u5b58\u56fe\u7247\u7684\u65b9\u6cd5\uff1a<\/p>\n

1. \u901a\u8fc7\u6293\u5305<\/strong><\/div>\n

selenium-wire \u662fselenium\u6269\u5c55\uff0c\u5b83\u53ef\u4ee5\u5bf9\u6240\u6709\u8bf7\u6c42\u6293\u5305\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u4fee\u6539\u8bf7\u6c42\u5934\uff0c\u8bf7\u6c42body\uff0c\u8bf7\u6c42\u8fd4\u56de\u503c\u7b49\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n

selenium-wire \u7684\u4f7f\u7528\u548cselenium\u4e00\u6837\uff0c\u4f60\u53ea\u4eceseleniumwire\u5bfc\u5165webdriver\u5c31\u884c\uff0c\u5bf9\u4e8e\u5176\u4ed6\u5305\u8fd8\u662f\u4eceselenium\u5bfc\u5165<\/p>\n

\r\nfrom selenium.webdriver.chrome.options import Options\r\nfrom seleniumwire.webdriver import Chrome\r\n \r\ndriver = Chrome(options= Options())<\/pre>\n

\u4e0b\u8f7d\u56fe\u7247\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n

1-1.\u901a\u8fc7\u62e6\u622a\u5668<\/strong><\/span><\/div>\n

\u901a\u8fc7\u62e6\u622a\u5668\u9884\u5148\u628a\u6240\u6709\u56fe\u7247\u4fdd\u5b58\u4e0b\u6765\uff0c\u8981\u7528\u5230\u65f6\u5728\u7f13\u5b58\u76ee\u5f55\u4e2d\u627e<\/p>\n

\r\ndef get_img_path_from_url(url):\r\n    # \u81ea\u884c\u5b9e\u73b0\r\n    return url\r\n \r\ndef response_interceptor(request, response):\r\n    t=response.headers['Content-Type']\r\n    if request.host=='xxx' and t and 'image' in t:\r\n        with open(get_img_path_from_url(request.url), 'wb') as f:\r\n            f.write(response.body)\r\n             \r\ndriver.response_interceptor = response_interceptor\r\n \r\ndriver.get('...')\r\nsrc=driver.find_element_by_tag_name('img').get_attribute('src')\r\nimg_path=get_img_path_from_url(src)<\/pre>\n
1-2. \u8bf7\u6c42\u540e\u5728\u6240\u6709\u8bf7\u6c42\u4e2d\u83b7\u53d6<\/strong><\/span><\/div>\n

\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e2a\u7f3a\u70b9\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u7f13\u5b58\u56fe\u7247\uff0c\u5982\u679c\u4e4b\u524d\u5df2\u7ecf\u7f13\u5b58\u8fc7\u8fd9\u5f20\u56fe\u7247\u662f\u4e0d\u4f1a\u6709\u7f51\u7edc\u8bf7\u6c42\u7684<\/p>\n

\r\n# \u4e0b\u8f7d\u524d\u5148\u6e05\u7406\u6570\u636e\uff0c\u4e0d\u7136\u8bf7\u6c42\u592a\u591a\r\ndel driver.requests\r\ndriver.get('...')\r\nsrc=driver.find_element_by_tag_name('img').get_attribute('src')\r\n\r\nfor r in driver.iter_requests():\r\n    if r.url==src:\r\n        with open('img', 'wb') as f:\r\n            f.write(r.response.body)<\/code><\/pre>\n
2. \u901a\u8fc7canvas<\/strong><\/div>\n

\u4f7f\u7528js\u628a\u56fe\u7247\u653e\u5230canvas\u4e2d\uff0c\u7136\u540e\u83b7\u53d6base64\u5b57\u7b26\u4e32\uff0c\u518d\u4fdd\u5b58<\/p>\n

\r\nimport base64\r\nimport os\r\nimport re\r\nfrom io import BytesIO\r\nfrom PIL import Image\r\n \r\ndef base64_to_image(base64_str):\r\n    base64_data = re.sub('^data:image\/.+;base64,', '', base64_str)\r\n    byte_data = base64.b64decode(base64_data)\r\n    image_data = BytesIO(byte_data)\r\n    img = Image.open(image_data)\r\n    return img\r\n \r\n \r\njs = \"let c = document.createElement('canvas');let ctx = c.getContext('2d');\" \\\r\n     \"let img = document.getElementsByTagName('img')[0]; \/*\u627e\u5230\u56fe\u7247*\/ \" \\\r\n     \"c.height=img.naturalHeight;c.width=img.naturalWidth;\" \\\r\n     \"ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);\" \\\r\n     \"let base64String = c.toDataURL();return base64String;\"\r\n      \r\nbase64_str = driver.execute_script(js)\r\nimg = base64_to_image(base64_str)\r\n \r\nimg.save('xx.png')<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8epython selenium\u4fdd\u5b58\u56fe\u7247\u6700\u597d\u7684\u4e24\u79cd\u65b9\u6cd5\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

selenium\u722c\u53d6\u9875\u9762\u65f6\u7ecf\u5e38\u9047\u5230\u8981\u4fdd\u5b58\u56fe\u7247\u7684\u9700\u6c42\uff0c\u901a\u5e38\u7684\u505a\u6cd5\u662f\u83b7\u53d6\u94fe\u63a5\u540e\u7528 requests \u4e0b\u8f7d\uff0c\u4f46\u8fd9\u79cd\u65b9 […]<\/p>\n","protected":false},"author":1482,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-234954","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=234954"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234954\/revisions"}],"predecessor-version":[{"id":234957,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234954\/revisions\/234957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=234954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=234954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=234954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}