-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtraction.py
54 lines (39 loc) · 1.61 KB
/
Extraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# extracting Data from the given links in input.xlsx
import pandas as pd
import requests
from bs4 import BeautifulSoup
import os
# Read Input Excel file
input_file = r"C:\Users\khanh\Desktop\learn_j\Blackcoffer\Solution\Input.xlsx"
df = pd.read_excel(input_file)
# Creating Output dir
output_dir = "extracted_articles"
os.makedirs(output_dir, exist_ok=True)
# Itreare over each row of dataFrame
for index, row in df.iterrows():
url_id = row['URL_ID']
url = row['URL']
try:
# Fetch the web page
response = requests.get(url)
response.raise_for_status()
# parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Extract the article title...
title = soup.find('title').get_text(strip=True) if soup.find('title') else 'No Title'
# Extract the article text
article_text = ""
for paragraph in soup.find_all('p'):
article_text += paragraph.get_text(strip=True) + "\n"
# Create the file name
file_name = f"{url_id}.txt"
file_path = os.path.join(output_dir, file_name)
# Write the title and article text to the file
with open(file_path, 'w', encoding='utf-8') as file:
file.write(f"Title: {title}\n\n")
file.write(article_text)
print(f"Successfully extracted and saved article for {url_id} to {file_path}")
except requests.RequestException as e:
print(f"Failed to fetch {url}: {e}")
except Exception as e:
print(f"AN error occurred: {e}")