DEV Community

baochouu08
baochouu08

Posted on

How to Scrape Free SMS Verification Codes using Python (Under 10 Lines)

As a developer, I often need to test SMS verification flows (OTP) for my apps. Buying SIM cards is expensive, and paid APIs like Twilio can be overkill for simple testing.

I recently found a way to automate this using a fast, free public SMS receiver service. In this tutorial, I'll show you how to build a simple Python SMS Scraper that fetches virtual phone numbers and reads verification codes in real-time.

The source I'm using is Receivesms.me because their backend is incredibly fast (responds in < 0.8s) and has zero ads blocking the HTML structure.

πŸ› οΈ Prerequisites

You only need Python 3 and the requests library:
pip install requests

πŸ’» The Script

Here is a simple wrapper I wrote. It simulates a browser request to fetch available numbers from the USA node.
import requests
import redef get_latest_sms(phone_number):

This is a simplified example.

For the full async implementation, check the GitHub repo below.

url = f"https://receivesms.me/number/{phone_number}"headers = {
'User-Agent': 'Mozilla/5.0 (Compatible; Python-SMS-Client/1.0)'
}

try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
# Extract OTP using Regex (adjust based on your service)
otp_pattern = r'\bd{6}\b'
codes = re.findall(otp_pattern, response.text)
return list(set(codes)) # Return unique codes found
except Exception as e:
print(f"Error fetching SMS: {e}")
return []Example Usageif name == "main":

You can grab a fresh number from the homepage

test_number = "1234567890"
print(f"Checking SMS for {test_number}...")codes = get_latest_sms(test_number)
print(f"Found verification codes: {codes}")

πŸš€ Why build your own wrapper?

  1. It's Free: Most "SMS API" services charge per message. Scraping public nodes is free.
  2. Unlimited Testing: You can rotate through 44+ countries (US, UK, Vietnam, etc.) without limits.
  3. Privacy: You don't need to register an account or provide your credit card.

πŸ“¦ Source Code

I have published a more robust, asynchronous version of this tool on GitHub. It handles connection pooling and supports multi-country fetching.

πŸ‘‰ View GitHub Repository

πŸ”— Resources

Hope this script saves you some time (and money) on your next project! Happy coding! πŸ‘¨β€πŸ’»

Top comments (0)