-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebscrapping_Selenium.py
68 lines (55 loc) · 2.3 KB
/
Webscrapping_Selenium.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pandas as pd
from bs4 import BeautifulSoup
from time import sleep
from selenium import webdriver
import sqlite3 as sql
urls = []
product_urls = []
list_of_reviews = []
# Each page urls
for i in range(1, 252):
urls.append(f"https://www.etsy.com/in-en/c/jewelry/earrings/ear-jackets-and-climbers?ref=pagination&explicit=1&page={i}")
# Scrapping each product's urls | 16,064 products
for url in urls:
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get(url)
sleep(5)
for i in range(1, 65):
product = driver.find_element_by_xpath(f'//*[@id="content"]/div/div[1]/div/div[3]/div[2]/div[2]/div[1]/div/div/ul/li[{i}]/div/a')
product_urls.append(product.get_attribute('href'))
# Scrapping each product's reviews
driver = webdriver.Chrome(executable_path='chromedriver.exe')
for product_url in product_urls[15:]:
try:
driver.get(product_url)
sleep(5)
html = driver.page_source
soup = BeautifulSoup(html,'html')
for i in range(4):
try:
list_of_reviews.append(soup.select(f'#review-preview-toggle-{i}')[0].getText().strip())
except:
continue
while(True):
try:
next_button = driver.find_element_by_xpath('//*[@id="reviews"]/div[2]/nav/ul/li[position() = last()]/a[contains(@href, "https")]')
if next_button != None:
next_button.click()
sleep(5)
html = driver.page_source
soup = BeautifulSoup(html,'html')
for i in range(4):
try:
list_of_reviews.append(soup.select(f'#review-preview-toggle-{i}')[0].getText().strip())
except:
continue
except Exception as e:
print('finsish : ', e)
break
except:
continue
scrappedReviewsAll = pd.DataFrame(list_of_reviews, index = None, columns = ['reviews'])
scrappedReviewsAll.to_csv('scrappedReviewsAll.csv')
df = pd.read_csv('scrappedReviewsAll.csv')
conn = sql.connect('scrappedReviewsAll.db')
df.to_sql('scrappedReviewsAllTable', conn)