-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
37 lines (32 loc) · 1011 Bytes
/
Copy pathfetch.py
File metadata and controls
37 lines (32 loc) · 1011 Bytes
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
import wikipedia
import os
# Create a folder to store the files
os.makedirs("ecofriendly_activities", exist_ok=True)
# List of eco-friendly topics (one per file)
topics = [
"Sustainable living",
"Renewable energy",
"Recycling",
"Zero-waste lifestyle",
"Solar power",
"Organic farming",
"Electric vehicles",
"Composting",
"Water conservation"
]
# Fetch and save each topic as a separate text file
for topic in topics:
try:
# Fetch Wikipedia page content
page = wikipedia.page(topic)
content = page.content
# Save to a text file
filename = f"ecofriendly_activities/{topic.replace(' ', '_')}.txt"
with open(filename, "w", encoding="utf-8") as f:
f.write(f"Title: {page.title}\n\n")
f.write(content)
print(f"Saved: {filename}")
except wikipedia.exceptions.PageError:
print(f"Page not found: {topic}")
except Exception as e:
print(f"Error fetching {topic}: {str(e)}")