-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_scraping_and_classification.py
More file actions
34 lines (29 loc) · 1.04 KB
/
web_scraping_and_classification.py
File metadata and controls
34 lines (29 loc) · 1.04 KB
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
import requests
from bs4 import BeautifulSoup
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Web Scraping Setup
url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
data = soup.find_all("div", class_="data")
# Data Preprocessing
dataset = []
for item in data:
feature1 = item.find("span", class_="feature1").text
feature2 = item.find("span", class_="feature2").text
label = item.find("span", class_="label").text
dataset.append([feature1, feature2, label])
df = pd.DataFrame(dataset, columns=["Feature1", "Feature2", "Label"])
X = df[["Feature1", "Feature2"]]
y = df["Label"]
# Model Training
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
# Model Evaluation
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)