-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebscrap.py
More file actions
51 lines (41 loc) · 1.73 KB
/
Webscrap.py
File metadata and controls
51 lines (41 loc) · 1.73 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
40
41
42
43
44
45
46
47
48
49
50
51
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
base_url = "https://www.amazon.in/s?k=phones"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
}
max_retries = 5
retry_delay = 5 # seconds
for retry in range(max_retries):
try:
response = requests.get(base_url, headers=headers)
response.raise_for_status()
break # Exit the loop if the request is successful
except requests.RequestException as e:
print(f"Request error (Retry {retry + 1}/{max_retries}):", e)
if retry < max_retries - 1:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
print("Max retries reached. Exiting.")
exit()
soup = BeautifulSoup(response.content, "html.parser")
phone_cards = soup.find_all("div", class_="s-result-item")
phone_data = []
for phone_card in phone_cards:
try:
phone_name = phone_card.find("span", class_="a-text-normal").text.strip()
phone_price = phone_card.find("span", class_="a-offscreen").text if phone_card.find("span", class_="a-offscreen") else "N/A"
phone_rating = phone_card.find("span", class_="a-icon-alt").text if phone_card.find("span", class_="a-icon-alt") else "N/A"
phone_data.append({
"Name": phone_name,
"Price": phone_price,
"Rating": phone_rating
})
except Exception as e:
print("Error in extracting phone data:", e)
df = pd.DataFrame(phone_data)
df.to_csv("amazon_phones.csv", index=False)
print("Data successfully extracted and CSV saved.")