-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatic_webscape.py
More file actions
39 lines (29 loc) · 1.21 KB
/
Static_webscape.py
File metadata and controls
39 lines (29 loc) · 1.21 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
35
36
37
38
39
from bs4 import BeautifulSoup
import pandas as pd
with open("Accessories And Gadgets.html", "r") as file:
html = BeautifulSoup(file, "html.parser")
product_names = html.find_all('a', class_='product-item__title text--strong link')
sale_prices = html.find_all('span', class_='product-label product-label--on-sale')
regular_prices = html.find_all('span', class_='price price--compare')
product_data = []
for index, name_tag in enumerate(product_names):
try:
product_name = name_tag.text.strip()
try:
sale_price = sale_prices[index].text.strip()
except IndexError:
sale_price = None # Sale price not available
try:
regular_price = regular_prices[index].text.strip()
except IndexError:
regular_price = None # Regular price not available
product_data.append({
'Product Name': product_name,
'Sale Price': sale_price,
'Regular Price': regular_price
})
except Exception as e:
print(f"Error processing product: {e}")
df = pd.DataFrame(product_data)
df.to_csv('scraped.csv', index=False)
print(f"Data saved to scraped_products.csv")