Skip to content

Instantly share code, notes, and snippets.

@nukeador
Created January 4, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nukeador/8f97afd8c1515ac6ac52f76bb8892410 to your computer and use it in GitHub Desktop.
Save nukeador/8f97afd8c1515ac6ac52f76bb8892410 to your computer and use it in GitHub Desktop.
Script that retrieves the sender and message of the last SMS received by a randomly selected phone number from a given country code
import requests
import random
from bs4 import BeautifulSoup
# Make sure you install BeautifulSoup first
# pip3 install beautifulsoup4
# Define a function to get a list of phone numbers in a given country
def get_numbers(country):
# Make a GET request to the website to get the HTML content
response = requests.get(f"https://online-sms.org/Free-{country}-Phone-Number")
# Initialize an empty list to store the phone numbers
numbers_list = []
# Split the HTML content into lines
for line in response.text.split("\n"):
# If the line contains the string "Receive SMS", it is likely to contain a phone number
if "Receive SMS" in line:
# Extract the phone number by keeping only the digits in the line
numbers_list.append(''.join(c for c in line if c.isdigit()))
# Return the list of phone numbers
return numbers_list
# Define a function to get the HTML content of the page containing the last SMS received by a given phone number
def get_last_sms(number):
# Construct the URL of the page
base_url = f"https://online-sms.org/free-phone-number-{number}"
# Make a GET request to the URL to get the HTML content
response = requests.get(base_url)
# Return the HTML content
return response.text
# Define a function to parse the HTML content and extract the sender and message of the last SMS received
def parse_last_sms(sms):
# Use Beautiful Soup to parse the HTML content
soup = BeautifulSoup(sms, "html.parser")
# Find all the rows in the table containing the SMS information
rows = soup.find_all("tr")
sender = None
message = []
# Find all the columns in the row
cols = rows[1].find_all("td")
# If the row has at least 3 columns, it is likely to contain the sender and message
if len(cols) >= 3:
# Extract the sender from the first column and the message from the second column
sender = cols[0].text.strip()
message.append(cols[1].text.strip())
# Return the sender and message as a tuple
return sender, "\n".join(message)
# Ask the user to enter a country code
country = input("Enter a country code (e.g. US): ")
# Get the list of phone numbers in the given country
numbers_list = get_numbers(country)
# Print the list of phone numbers
print("Numbers from", country + ":")
print(', '.join(numbers_list))
# Select a random phone number from the list
number = random.choice(numbers_list)
# Print the selected phone number
print("Randomly selected number:", number)
# Get the HTML content of the page containing the last SMS received by the selected phone number
last_sms = get_last_sms(number)
# Parse the HTML content to extract the sender and message of the last SMS received
sender, message = parse_last_sms(last_sms)
# Print the sender and message
print("Sender:", sender)
print("Message:")
print(message)
# Ask the user if they want to keep receiving messages from this phone number
repeat = input("Keep receiving messages from this number? Y/N: ")
# Keep looping as long as the user wants to receive messages
while repeat.upper() == "Y":
# Get the HTML content of the page containing the last SMS received by the selected phone number
last_sms = get_last_sms(number)
# Parse the HTML content to extract the sender and message of the last SMS received
sender, message = parse_last_sms(last_sms)
# Print the sender and message
print("Sender:", sender)
print("Message:")
print(message)
# Ask the user if they want to keep receiving messages from this phone number
repeat = input("Keep receiving messages from this number? Y/N: ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment