{"id":344,"date":"2018-05-27T14:20:11","date_gmt":"2018-05-27T06:20:11","guid":{"rendered":"http:\/\/davidblus.top\/wordpress\/?p=344"},"modified":"2018-09-22T14:54:19","modified_gmt":"2018-09-22T06:54:19","slug":"li_yong_selenium","status":"publish","type":"post","link":"http:\/\/davidblus.top\/wordpress\/index.php\/2018\/05\/27\/li_yong_selenium\/","title":{"rendered":"\u5229\u7528selenium\u81ea\u52a8\u5316\u8bbf\u95ee\u7f51\u7ad9\u5e76\u7b80\u5355\u4f7f\u7528pytesseract\u8bc6\u522b\u9a8c\u8bc1\u7801"},"content":{"rendered":"<p>\u672c\u7bc7\u535a\u5ba2\u4e3b\u8981\u662f\u8bb0\u5f55\u4e00\u4e0b\u4f7f\u7528Python\u7684selenium\u5e93\u7684\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u8bc6\u522b\u9a8c\u8bc1\u7801\u65f6\u9047\u5230\u7684\u5c0f\u5751\u3002\u6211\u5148\u6765\u5217\u4e3e\u51e0\u4e2a\u53ef\u4f9b\u5b66\u4e60\u7684\u6587\u6863\uff0c\u4e3b\u8981\u5206\u4e3a\u4e24\u7c7b\u3002<\/p>\n<p>\u4e00\u7c7b\u662f\u5b66\u4e60selenium\u81ea\u52a8\u5316\u8bbf\u95ee\u7f51\u7ad9\uff1a<br \/>\n<a href=\"http:\/\/selenium-python.readthedocs.io\/\" rel=\"noopener\" target=\"_blank\">Selenium with Python<\/a><br \/>\n<a href=\"http:\/\/selenium-python-docs-zh.readthedocs.io\/zh_CN\/latest\/\" rel=\"noopener\" target=\"_blank\">Selenium Python \u6587\u6863<\/a><br \/>\n<a href=\"https:\/\/seleniumhq.github.io\/selenium\/docs\/api\/py\/api.html\" rel=\"noopener\" target=\"_blank\">Selenium Documentation<\/a><\/p>\n<p>\u4e00\u7c7b\u662f\u8bc6\u522b\u9a8c\u8bc1\u7801\u76f8\u5173\uff1a<br \/>\n<a href=\"https:\/\/github.com\/tesseract-ocr\/tesseract\" rel=\"noopener\" target=\"_blank\">tesseract<\/a><\/p>\n<p>\u76ee\u6807\u7ad9\u70b9\u662fYunFile\u7f51\u76d8\uff0c\u6211\u60f3\u8981\u81ea\u52a8\u666e\u901a\u4e0b\u8f7d\u7ed9\u51fa\u7684\u67d0\u4e2a\u6587\u4ef6\uff0c\u6bd4\u5982\u8fd9\u4e2aurl\uff1a<a href=\"http:\/\/tadown.com\/fs\/1q8q3_d3a51081cBEFF986\/\" rel=\"noopener\" target=\"_blank\">http:\/\/tadown.com\/fs\/1q8q3_d3a51081cBEFF986\/<\/a><br \/>\n\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"python\">from PIL import Image, ImageEnhance\nimport pytesseract\nimport re\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.common.keys import Keys\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\nimport time\n\nCHROME_DRIVER_PATH = '.\/chromedriver'\n\n\n# \u83b7\u53d6\u9a8c\u8bc1\u7801\ndef get_code_from_image(code_image_path):\n    code_img = Image.open(code_image_path)\n    # numbers\u4e3a\u914d\u7f6e\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3a\uff1atessedit_char_whitelist 0123456789\n    code_text = pytesseract.image_to_string(code_img, config='--psm 7 numbers').strip()\n    # \u53bb\u6389\u975e\u6570\u5b57\u5b57\u7b26\n    code_text = re.sub(\"\\D\", \"\", code_text)\n    return code_text\n\n\ndef main():\n    # \u914d\u7f6eChrome\u6d4f\u89c8\u5668\uff0cprofile.default_content_settings.popups=0\u8868\u793a\u7981\u6b62\u5f39\u51fa\u7a97\u53e3\n    chrome_options = webdriver.ChromeOptions()\n    preferences = {'profile.default_content_settings.popups': 0, }\n    chrome_options.add_experimental_option('prefs', preferences)\n\n    driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH, options=chrome_options)\n    driver.maximize_window()\n    driver.implicitly_wait(10)\n    driver.set_page_load_timeout(10)\n    driver.set_script_timeout(10)\n    url = 'http:\/\/tadown.com\/fs\/1q8q3_d3a51081cBEFF986\/'\n\n    try:\n        driver.get(url)\n        useless_element = WebDriverWait(driver, 10).until(\n            EC.presence_of_element_located((By.CLASS_NAME, 'slow_button'))\n        )\n        download_element = driver.find_elements_by_class_name('slow_button')[1]\n        download_element.click()\n        dialog_close_element = driver.find_elements_by_class_name('ui_dialog_close')[1]\n        dialog_close_element.click()\n        driver.save_screenshot('screenshot1.png')\n\n        # \u83b7\u53d6\u9a8c\u8bc1\u7801\u56fe\u7247\uff0c\u5904\u7406\u5e76\u4fdd\u5b58\n        code_image_element = driver.find_element_by_id('cvimg2')\n        # \u83b7\u53d6\u9a8c\u8bc1\u7801\u56fe\u7247\u7684\u5927\u5c0f\n        imgSize = code_image_element.size\n        # \u83b7\u53d6\u9a8c\u8bc1\u7801\u5143\u7d20\u5750\u6807\n        imgLocation = code_image_element.location\n        # \u8ba1\u7b97\u9a8c\u8bc1\u7801\u6574\u4f53\u5750\u6807\n        rangle_normal = (int(imgLocation['x']), int(imgLocation['y']),\n                  int(imgLocation['x'] + imgSize['width']), int(imgLocation['y'] + imgSize['height']))\n        # \u5b9e\u9a8c\u53d1\u73b0\uff0c\u622a\u56fe\u5f97\u5230\u7684\u4f4d\u7f6e\u662f\u539f\u59cb\u4f4d\u7f6e\u7684\u4e24\u500d\uff0c\u539f\u56e0\u672a\u77e5\uff01\uff01\uff01\n        rangle = (rangle_normal[0] * 2, rangle_normal[1] * 2, rangle_normal[2] * 2, rangle_normal[3] * 2)\n        code_image = Image.open('screenshot1.png').crop(rangle)\n        code_image.save('code.png')\n        # \u8f6c\u5316\u4e3a\u7070\u5ea6\u56fe\n        code_grey_image = code_image.convert('L')\n        code_grey_image.save('code_grey.png')\n        # \u589e\u52a0\u5bf9\u6bd4\u5ea6\n        code_sharp_image = ImageEnhance.Contrast(code_image).enhance(2.0)\n        code_sharp_image.save('code_sharp.png')\n        # \u8f6c\u5316\u4e3a\u4e8c\u503c\u5316\u56fe\u7247\n        code_bin_image = code_image.convert('1')\n        code_bin_image.save('code_bin.png')\n\n        # \u5f97\u5230\u9a8c\u8bc1\u7801 code\uff0c\u5fc5\u987b\u662f4\u4f4d\u957f\u5ea6\n        code = get_code_from_image('code.png')\n        print('code:', code)\n        if len(code) != 4:\n            code = get_code_from_image('code_grey.png')\n            print('code_grey:', code)\n            if len(code) != 4:\n                code = get_code_from_image('code_sharp.png')\n                print('code_sharp:', code)\n                if len(code) != 4:\n                    code = get_code_from_image('code_bin.png')\n                    print('code_bin:', code)\n                    if len(code) != 4:\n                        print('Can not identify the verification code correctly!!!')\n                        return\n\n        code_input_element = driver.find_element_by_id('vcode')\n        code_input_element.clear()\n        code_input_element.send_keys(code)\n        code_input_element.send_keys(Keys.RETURN)\n\n        time.sleep(32)\n        driver.save_screenshot('screenshot2.png')\n        import pdb\n        pdb.set_trace()\n\n    finally:\n        driver.quit()\n\n\nif __name__ == '__main__':\n    main()\n\n<\/code><\/pre>\n<p>\u5176\u4e2d\u7684\u4ee3\u7801\u5927\u591a\u662f\u4ece\u7f51\u4e0a\u627e\u7684\uff0c\u53ea\u6709\u4e00\u4e2a\u5730\u65b9\u548c\u7f51\u4e0a\u4e0d\u540c\uff0c\u6211\u8ba4\u4e3a\u8fd9\u662f\u4e2a\u5c0f\u5751\uff0c\u90a3\u5c31\u662f\u901a\u8fc7\u7f51\u7ad9\u9a8c\u8bc1\u7801\u5143\u7d20\u800c\u83b7\u53d6\u7684\u9a8c\u8bc1\u7801\u5143\u7d20\u5750\u6807\u4e0e\u622a\u56fe\u4e2d\u9a8c\u8bc1\u7801\u7684\u5750\u6807\u4f4d\u7f6e\u4e0d\u540c\uff0c\u8fd8\u597d\u8ba9\u6211\u53d1\u73b0\u662f\u4e8c\u500d\u7684\u5173\u7cfb\uff0c\u539f\u56e0\u7684\u8bdd\u8fd8\u4e0d\u6e05\u695a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u535a\u5ba2\u4e3b\u8981\u662f\u8bb0\u5f55\u4e00\u4e0b\u4f7f\u7528Python\u7684selenium\u5e93\u7684\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u8bc6\u522b\u9a8c\u8bc1\u7801\u65f6\u9047\u5230\u7684\u5c0f\u5751\u3002\u6211\u5148\u6765\u5217\u4e3e\u51e0\u4e2a\u53ef &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/davidblus.top\/wordpress\/index.php\/2018\/05\/27\/li_yong_selenium\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5229\u7528selenium\u81ea\u52a8\u5316\u8bbf\u95ee\u7f51\u7ad9\u5e76\u7b80\u5355\u4f7f\u7528pytesseract\u8bc6\u522b\u9a8c\u8bc1\u7801\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-wang_zhan_pa_qu","entry"],"_links":{"self":[{"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":3,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":479,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/344\/revisions\/479"}],"wp:attachment":[{"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/davidblus.top\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}