import sys import requests from bs4 import BeautifulSoup def get_title_from_url(url): try: response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') title_tag = soup.find('title') if title_tag: title = title_tag.string.strip() else: title_tag = soup.find('h1') if title_tag: title = title_tag.string.strip() else: title = "No title found" return title except Exception as e: print(f"Error: {e}") return None if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python3 parse.py ") sys.exit(1) filename = sys.argv[1] # Membaca daftar URL dari file yang diberikan with open(filename, 'r') as file: urls = file.readlines() # Membuka file untuk menyimpan hasil with open('result.txt', 'w') as result_file: for url in urls: url = url.strip() title = get_title_from_url(url) result_file.write(f"{url}|{title}\n")