-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigi.py
68 lines (51 loc) · 1.8 KB
/
digi.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
from bs4 import BeautifulSoup
import pandas as pd
import requests
import re
import csv
import json
url = "https://www.digikala.com/incredible-offers/"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
all=[]
products_list = soup.find_all("div", class_="c-product-list__item js-product-list-content")
for i in products_list:
title = i.find('div', class_ = "js-product-cart").get('title')
price = re.sub(r"['\n'](\D)+\s","",i.find('div', class_ = "c-price__value-wrapper js-product-card-price").text).replace("تومان","")
# end = i.find('div', class_ = "c-promotion__badge c-promotion__badge--incredible-over")
try:
sale = i.find('div', class_ = "c-price__discount-oval").find('span').text
all.append(dict({"title": title , "price": price , "sale": sale}))
except:
pass
# all.append(dict({"title": title , "price": price , "sale": sale}))
print(all)
df = pd.DataFrame(all)
print(df)
df.to_csv('products.csv')
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['id']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code
# Decide the two file paths according to your
# computer system
csvFilePath = r'product_detail.csv'
jsonFilePath = r'Names.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)