{"id":1064,"date":"2023-04-16T22:11:00","date_gmt":"2023-04-16T16:41:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1064"},"modified":"2023-08-15T13:30:38","modified_gmt":"2023-08-15T08:00:38","slug":"solve-captcha-in-python-using-2captcha","status":"publish","type":"post","link":"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha","title":{"rendered":"How To Solve CAPTCHA In A Few Steps In Python Using 2captcha"},"content":{"rendered":"\n<p>You may have solved millions of captchas for verification, which can be a tedious chore when you need to authenticate your identity as a human. These websites offer an additional layer of security by utilizing captcha services to prevent bots and automation scripts from accessing their website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-what-is-captcha\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-what-is-captcha\"><\/a>What is CAPTCHA?<\/h2>\n\n\n\n<p>CAPTCHA (<strong>Completely Automated Public Turing Test to Tell Computers and Humans Apart<\/strong>) is a form of challenge-response test used to assess whether or not a user is human.<\/p>\n\n\n\n<p>CAPTCHAs display jumbled, scrambled, or distorted text, images, numbers, and audio that is tough for computers to solve but relatively easy for people to solve. These are used to protect websites and applications against harmful actions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-2captcha-api\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-2captcha-api\"><\/a>2captcha API<\/h2>\n\n\n\n<p>There are numerous applications and services available to assist us in solving a captcha in seconds. 2captcha is a website that offers captcha-solving services.<\/p>\n\n\n\n<p>2captcha\u00a0provides API for various programming languages, including Python, and we&#8217;ll utilize the\u00a0<code>2captcha-python<\/code>\u00a0library to solve the captcha in a few steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-installing-dependencies\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-installing-dependencies\"><\/a>Installing dependencies<\/h2>\n\n\n\n<p>The primary work is to install the Python library called&nbsp;<code>2captcha-python<\/code>. Run the following command in your terminal.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">pip install 2captcha-python<\/pre><\/div>\n\n\n\n<p>Please keep in mind that we are using&nbsp;<strong><em>pip<\/em><\/strong>&nbsp;to install the library. If you are not using&nbsp;<strong><em>pip<\/em><\/strong>, the installation command will be different.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-solving-recaptcha\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-solving-recaptcha\"><\/a>Solving reCAPTCHA<\/h2>\n\n\n\n<p>We&#8217;ll use a reCAPTCHA testing website and here&#8217;s the URL of the website&nbsp;<a target=\"_blank\" href=\"https:\/\/patrickhlauke.github.io\/recaptcha\/\" rel=\"noreferrer noopener\">https:\/\/patrickhlauke.github.io\/recaptcha\/<\/a>. It provides a reCAPTCHA widget for testing purposes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"257\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/recaptcha.png\" alt=\"reCAPTCHA testing website\" class=\"wp-image-1067\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/recaptcha.png 582w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/recaptcha-300x132.png 300w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-code\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-code\"><\/a>Code<\/h3>\n\n\n\n<p>First, we must import the library and create an instance of the class&nbsp;<code>TwoCaptcha<\/code>, which we will initialise with the&nbsp;<strong>API key<\/strong>. To make a&nbsp;<strong>POST<\/strong>&nbsp;request to the target site, we also need the&nbsp;<code>requests<\/code>&nbsp;library.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Imported the required libs\nfrom twocaptcha import TwoCaptcha\nimport requests\n\n# Instance of TwoCaptcha\nsolver = TwoCaptcha('YOUR_API_KEY')<\/pre><\/div>\n\n\n\n<p>After we&#8217;ve built the instance, the next step is to obtain the token from the 2captcha server.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Target website URL\nsite_url = \"https:\/\/patrickhlauke.github.io\/recaptcha\/\"\n\n# Getting the token\ntry:\n    # For solving reCAPTCHA\n    token = captcha_solver.recaptcha(\n        sitekey='6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5',\n        url=site_url\n    )\n\n# Handling the exceptions\nexcept Exception as e:\n    raise SystemExit('Error: CAPTCHA token not recieved.')\n\n# Print the token and exit the program\nelse:\n    SystemExit('Token- ' + str(token))<\/pre><\/div>\n\n\n\n<p>We called the&nbsp;<code>recaptcha<\/code>&nbsp;method from the instance&nbsp;<code>captcha_solver<\/code>&nbsp;within the&nbsp;<code>try<\/code>&nbsp;block and passed the&nbsp;<strong>reCAPTCHA data-sitekey<\/strong>&nbsp;to the&nbsp;<code>sitekey<\/code>&nbsp;parameter and the&nbsp;<strong>URL<\/strong>&nbsp;of the target site to the&nbsp;<code>url<\/code>&nbsp;parameter.<\/p>\n\n\n\n<p>To get the&nbsp;<code>sitekey<\/code>&nbsp;from the website, open it in inspect mode and look for the tag&nbsp;<code>&lt;iframe&gt;<\/code>&nbsp;with&nbsp;<code>title=\"reCAPTCHA\"<\/code>&nbsp;and you&#8217;ll find the key within the&nbsp;<code>src<\/code>&nbsp;attribute.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1352\" height=\"397\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/captcha-sitekey.png\" alt=\"reCAPTCHA sitekey\" class=\"wp-image-1068\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/captcha-sitekey.png 1352w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/captcha-sitekey-300x88.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/captcha-sitekey-1024x301.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/captcha-sitekey-768x226.png 768w\" sizes=\"auto, (max-width: 1352px) 100vw, 1352px\" \/><\/figure>\n\n\n\n<p>If any exceptions are triggered within the except block, the program will display an error message and exit. If the program is successfully executed, it will return the token that will be used to solve reCAPTCHA and then quit.<\/p>\n\n\n\n<p>The final step will be to transfer the token to the target site, which will be accomplished by sending a&nbsp;<code>POST<\/code>&nbsp;request to the target site via the&nbsp;<code>requests<\/code>&nbsp;library.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Imported the required libs\nfrom twocaptcha import TwoCaptcha\nimport requests\n\n# Instance of TwoCaptcha\ncaptcha_solver = TwoCaptcha('YOUR_API_KEY')\n\n# Target website URL\nsite_url = \"https:\/\/patrickhlauke.github.io\/recaptcha\/\"\n\n# Getting the token\ntry:\n    # For solving reCAPTCHA\n    token = captcha_solver.recaptcha(\n        sitekey='6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5',\n        url=site_url\n    )\n\n# Handling the exceptions\nexcept Exception as e:\n    raise SystemExit('Error: CAPTCHA token not recieved.')\n\n# Sending the token to the website\nelse:\n    # Sending spoof user-agent\n    headers = {'user-agent': 'Mozilla\/5.0 Chrome\/52.0.2743.116 Safari\/537.36'}\n\n    # Sending recieved token\n    data = {'recaptcha-token': str(token)}\n\n    # Making POST request to the target site\n    token_response = requests.post(site_url, headers=headers, data=data)\n\n    print('Token sent')<\/pre><\/div>\n\n\n\n<p>We changed the&nbsp;<code>else<\/code>&nbsp;part of the code, and the result is the code seen above. Before performing the&nbsp;<code>POST<\/code>&nbsp;request to the target site, we&#8217;ve established certain configurations to send along with the request within the&nbsp;<code>else<\/code>&nbsp;block.<\/p>\n\n\n\n<p>We used the&nbsp;<code>requests<\/code>&nbsp;library&#8217;s&nbsp;<code>post<\/code>&nbsp;function to send a&nbsp;<strong>POST<\/strong>&nbsp;request to the target site, passing the&nbsp;<code>site_url<\/code>&nbsp;(target site URL),&nbsp;<code>headers<\/code>&nbsp;(containing the user-agent), and&nbsp;<code>data<\/code>&nbsp;(containing token). The data can contain more parameters depending on the form.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note: The received token from the 2captcha servers will only be valid for 120 seconds, so you need to submit the token to the target site within the time limit.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-solving-hcaptcha\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-solving-hcaptcha\"><\/a>Solving hCAPTCHA<\/h2>\n\n\n\n<p>The URL of the hCAPTCHA demo site is&nbsp;<a target=\"_blank\" href=\"https:\/\/accounts.hcaptcha.com\/demo\" rel=\"noreferrer noopener\">https:\/\/accounts.hcaptcha.com\/demo<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"596\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchademo.png\" alt=\"hCAPTCHA Demo site\" class=\"wp-image-1069\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchademo.png 559w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchademo-281x300.png 281w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/figure>\n\n\n\n<p>The procedure will be the same as with reCAPTCHA, with the exception that we must modify the method to&nbsp;<code>hcaptcha<\/code>&nbsp;from the&nbsp;<code>2captcha-python<\/code>&nbsp;package.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Imported the required libs\nfrom twocaptcha import TwoCaptcha\nimport requests\n\n# Instance of TwoCaptcha\ncaptcha_solver = TwoCaptcha('YOUR_API_KEY')\n\n# Target website URL\nsite_url = \"https:\/\/accounts.hcaptcha.com\/demo\"\n\n# Getting the token\ntry:\n    # For solving hCAPTCHA\n    token = captcha_solver.hcaptcha(\n        sitekey='a5f74b19-9e45-40e0-b45d-47ff91b7a6c2',\n        url=site_url\n    )\n\n# Handling the exceptions\nexcept Exception as e:\n    raise SystemExit('Error: CAPTCHA token not recieved.')\n\n# Sending the token to the website\nelse:\n    # Sending spoof user-agent\n    headers = {'user-agent': 'Mozilla\/5.0 Chrome\/52.0.2743.116 Safari\/537.36'}\n\n    # Sending recieved token\n    data = {'hcaptcha-token': str(token)}\n\n    # Making POST request to the target site\n    token_response = requests.post(site_url, headers=headers, data=data)\n\n    print('Token sent')<\/pre><\/div>\n\n\n\n<p>We&#8217;ll get the&nbsp;<code>sitekey<\/code>&nbsp;of the&nbsp;<strong>hCAPTCHA<\/strong>&nbsp;within the&nbsp;<code>src<\/code>&nbsp;attribute of the&nbsp;<code>&lt;iframe&gt;<\/code>&nbsp;tag.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1349\" height=\"399\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchasitekey.png\" alt=\"hCAPTCHA sitekey\" class=\"wp-image-1070\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchasitekey.png 1349w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchasitekey-300x89.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchasitekey-1024x303.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hcaptchasitekey-768x227.png 768w\" sizes=\"auto, (max-width: 1349px) 100vw, 1349px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/solve-captcha-in-python-using-2captcha#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>We&#8217;ve used the&nbsp;<code>2captcha-python<\/code>&nbsp;package for solving the reCAPTCHA and hCAPTCHA. The&nbsp;<code>2captcha-python<\/code>&nbsp;package provided by&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/2captcha.com\/\" target=\"_blank\">2captcha<\/a>&nbsp;provides captcha-solving services and almost all types of captcha can be solved using their APIs. They provide a browser extension for solving CAPTCHAs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may have solved millions of captchas for verification, which can be a tedious chore when you need to authenticate your identity as a human. These websites offer an additional layer of security by utilizing captcha services to prevent bots and automation scripts from accessing their website. What is CAPTCHA? CAPTCHA (Completely Automated Public Turing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1066,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[60,12,31],"class_list":["post-1064","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-2captcha","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1064\/revisions\/1297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1066"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}