Forum Discussion
Help with Introduction to Python Scripting: Ep.7 – Demonstrate Your Skills
- 2 months 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.
You're supposed to download the web page recursively, i.e. all pages as well, e.g. chariry.html etc.
On the main page there are no phone numbers, that's right. On the other pages there are a few.
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)}")
- netcat2 months ago
Silver III
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.- SamDickison30 days ago
Community Manager
DCadet How'd you get on? Manage to get the correct answer in the end?
- DCadet29 days ago
Bronze II
yes I manage to get the right answer