Skip to content

Commit 1a350f0

Browse files
authored
Add Web_To_Excel Python Code
1 parent 6c1fcbf commit 1a350f0

2 files changed

Lines changed: 114 additions & 65 deletions

File tree

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
1-
# Web to Excel Data Automation
2-
3-
This Python project demonstrates how to extract a numeric value from a webpage using `requests` and `BeautifulSoup`, process it, and then write the result to a specific cell in an Excel file using `openpyxl`.
4-
5-
It is ideal for automating tasks like tracking online financial or statistical data and recording it directly into Excel spreadsheets.
6-
7-
---
8-
9-
## 📌 Features
10-
11-
- Fetches numerical data from a webpage.
12-
- Parses HTML tables and extracts values.
13-
- Converts annual values to monthly averages (customizable).
14-
- Writes the result into a specified Excel cell.
15-
16-
---
17-
18-
## 🚀 Requirements
19-
20-
Install required Python packages:
21-
22-
```bash
23-
pip install requests beautifulsoup4 openpyxl
24-
📂 File Structure
25-
bash
26-
27-
project-folder/
28-
├── web_to_excel.py # Main script file
29-
└── README.md # Documentation
30-
🧩 How to Use
31-
Update the script:
32-
33-
In the web_to_excel.py file, locate this line:
34-
35-
python
36-
37-
url = "ENTER_THE_WEBPAGE_URL_TO_SCRAPE"
38-
Replace it with the actual URL of the website you want to scrape data from.
39-
40-
Customize HTML parsing logic
41-
If the structure of the table is different, update the parsing logic accordingly.
42-
43-
Set your Excel file path and sheet name:
44-
45-
python
46-
47-
file_path = r"PATH_TO_YOUR_EXCEL_FILE.xlsx"
48-
sheet_name = "YourSheetName"
49-
Run the script:
50-
51-
bash
52-
53-
python web_to_excel.py
54-
⚠️ Notes
55-
Make sure the Excel file is closed before running the script.
56-
57-
If the webpage structure changes, the script may need to be updated.
58-
59-
This example assumes the target data is inside the first HTML table.
60-
61-
📄 License
62-
MIT License
63-
You are free to use, modify, and distribute this code with attribution.
64-
65-
© 2025 Data Solutions Lab. by Osman Uluhan – All rights reserved.
1+
# Web to Excel Data Automation
2+
3+
This Python project demonstrates how to extract a numeric value from a webpage using `requests` and `BeautifulSoup`, process it, and then write the result to a specific cell in an Excel file using `openpyxl`.
4+
5+
It is ideal for automating tasks like tracking online financial or statistical data and recording it directly into Excel spreadsheets.
6+
7+
---
8+
9+
## 📌 Features
10+
11+
- Fetches numerical data from a webpage.
12+
- Parses HTML tables and extracts values.
13+
- Converts annual values to monthly averages (customizable).
14+
- Writes the result into a specified Excel cell.
15+
16+
---
17+
18+
## 🚀 Requirements
19+
20+
Install required Python packages:
21+
22+
```bash
23+
pip install requests beautifulsoup4 openpyxl
24+
📂 File Structure
25+
bash
26+
Kodu kopyala
27+
project-folder/
28+
├── web_to_excel.py # Main script file
29+
└── README.md # Documentation
30+
🧩 How to Use
31+
Update the script:
32+
33+
In the web_to_excel.py file, go to the following line:
34+
35+
python
36+
Kodu kopyala
37+
url = "ENTER_THE_WEBPAGE_URL_TO_SCRAPE"
38+
Replace it with the actual URL of the website you want to scrape data from.
39+
40+
Customize HTML parsing logic
41+
If the structure of the table is different, you may need to update how the table is parsed using BeautifulSoup.
42+
43+
Set the correct Excel path and sheet name:
44+
45+
Update the following section with your file path and sheet name:
46+
47+
python
48+
Kodu kopyala
49+
file_path = r"PATH_TO_YOUR_EXCEL_FILE.xlsx"
50+
sheet_name = "YourSheetName"
51+
Run the script:
52+
53+
bash
54+
Kodu kopyala
55+
python web_to_excel.py
56+
⚠️ Notes
57+
Ensure the Excel file is closed before running the script.
58+
59+
This script assumes that the target data is in the first HTML table of the page.
60+
61+
The numeric format uses commas (,) for decimals. If your data uses dots (.), you may adjust the conversion logic.
62+
63+
📄 License
64+
MIT License
65+
You are free to use, modify, and distribute this code with attribution.
66+
67+
© 2025 Data Solutions Lab. by Osman Uluhan – All rights reserved.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
from openpyxl import load_workbook
4+
5+
# 1. Fetch data from a webpage and convert the annual value to a monthly average
6+
def fetch_annual_value():
7+
url = "ENTER_THE_WEBPAGE_URL_TO_SCRAPE" # Replace with the actual data source URL
8+
headers = {
9+
"User-Agent": "Mozilla/5.0"
10+
}
11+
resp = requests.get(url, headers=headers)
12+
resp.raise_for_status()
13+
soup = BeautifulSoup(resp.text, "html.parser")
14+
15+
# Get the first table on the page (assuming it contains the target data)
16+
table = soup.find("table")
17+
if table is None:
18+
raise Exception("Table not found on the page.")
19+
20+
# Loop through rows to find the first valid annual data
21+
for tr in table.find_all("tr"):
22+
tds = tr.find_all("td")
23+
if len(tds) >= 2:
24+
date_text = tds[0].text.strip()
25+
annual_value_text = tds[1].text.strip()
26+
try:
27+
# Convert comma to dot, cast to float, divide by 12, round to 2 decimals
28+
value = float(annual_value_text.replace(",", "."))
29+
monthly_average = round(value / 12, 2)
30+
return monthly_average
31+
except ValueError:
32+
raise Exception(f"Could not convert '{annual_value_text}' to float.")
33+
34+
raise Exception("Annual value not found in the table.")
35+
36+
# 2. Write the value to a specific cell in an Excel file
37+
def write_to_excel(value):
38+
file_path = r"C:\Users\Osman ULUHAN\Desktop\Borsa\Algoritmik Trading\250930 Spot & Viop Canlı Algo Takip Tablosu.xlsx"
39+
sheet_name = "Analiz"
40+
41+
wb = load_workbook(file_path)
42+
if sheet_name not in wb.sheetnames:
43+
raise Exception(f"Sheet '{sheet_name}' not found. Available sheets: {wb.sheetnames}")
44+
45+
ws = wb[sheet_name]
46+
ws["C25"] = value
47+
wb.save(file_path)

0 commit comments

Comments
 (0)