How You Get Readymade Python from chatGPT - After a Few Tries 😊
Sure! ... but, that dumped out all links.
No bueno😒 When I ran as python3 links.py url blogspot, it dumped out as many blogspot.com (that's it, per line) as there were links..
And that worked perfectly..
(click below to select the code) (howto for the click to select)
import sys import requests from bs4 import BeautifulSoup from urllib.parse import urlparse def get_links_with_domain(url, domain): try: response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.content, 'html.parser') parsed_domain = f".{domain}." links = [] for link in soup.find_all('a', href=True): if parsed_domain in urlparse(link['href']).netloc.lower(): links.append(link['href']) return links except requests.exceptions.RequestException as e: print(f"Error fetching the URL: {e}") return [] if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python3 script.py URL DOMAIN") sys.exit(1) url = sys.argv[1] domain = sys.argv[2] links_with_domain = get_links_with_domain(url, domain) if links_with_domain: print("var linksArray = [") for link in links_with_domain: print(f' "{link}",') print("];") else: print(f"No links found on the page containing '{domain}' as a whole word.")
Comments
Post a Comment