Forum Discussion
Help with Introduction to Python Scripting: Ep.7 – Demonstrate Your Skills
- 30 days ago
Perfect code. However, the answer field expects you to count also duplicates. I didn't notice this in the first place, because I did just a wget, then grep "+" and counted manually.
Generally, it's best to get a local copy first (that can be analyzed manually), and then implement automatic analysis. If something goes wrong, you can always take a look at the local copy.
Thank you for your insight, I got 14, but it's still wrong
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import urljoin
visited = set()
phone_numbers = set()
def scrape(url):
if url in visited:
return
visited.add(url)
print(f"Scraping {url}")
try:
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
# Find 12-digit numbers starting with +
matches = re.findall(r'\+\d{12}', r.text)
phone_numbers.update(matches)
# Recurse into internal links
for link in soup.find_all('a', href=True):
full_url = urljoin(url, link['href'])
if full_url.startswith(url): # ensure we stay within the same site
scrape(full_url)
except Exception as e:
print(f"Error scraping {url}: {e}")
start_url = f"http://10.102.50.198:{801}/"
scrape(start_url)
print("Extracted phone numbers:")
for number in phone_numbers:
print(number)
print(f"Total unique phone numbers found: {len(phone_numbers)}")
Perfect code. However, the answer field expects you to count also duplicates. I didn't notice this in the first place, because I did just a wget, then grep "+" and counted manually.
Generally, it's best to get a local copy first (that can be analyzed manually), and then implement automatic analysis. If something goes wrong, you can always take a look at the local copy.
- SamDickison26 days ago
Community Manager
DCadet How'd you get on? Manage to get the correct answer in the end?
- DCadet26 days ago
Bronze II
yes I manage to get the right answer