-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
executable file
·49 lines (44 loc) · 1.72 KB
/
script.py
File metadata and controls
executable file
·49 lines (44 loc) · 1.72 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
#!/usr/bin/python3
import requests as rq
import pandas as pd
import json
#https://www.cargurus.ca/Cars/detailListingJson.action?inventoryListing=323959470
offset = 0
def save_data(line):
with open("result.csv", 'a') as f:
f.write(line + "\n")
def clean(text):
return text.replace('\n','').replace('\t','').replace(',','')
while offset <= 7280:
url = f"https://www.cargurus.ca/Cars/searchResults.action?zip=T5L&inventorySearchWidgetType=BODYSTYLE&searchId=ca60ef5e-d977-4adb-894a-db1425b4f7dc&nonShippableBaseline=0&shopByTypes=NEAR_BY&sortDir=ASC&sourceContext=carGurusHomePageBody&distance=500&sortType=DEAL_SCORE&entitySelectingHelper.selectedEntity=bg5&offset={offset}&maxResults=15&filtersModified=true"
try:
pg = rq.get(url)
data = json.loads(pg.text)
except:
try:
pg = rq.get(url)
data = json.loads(pg.text)
except:
print("Error loading search")
break
for d in data:
print(f"offset: {offset}: {d['carYear']} {d['makeName']} {d['modelName']} {d['id']}")
year = d['carYear']
make = clean(d['makeName'])
model = clean(d['modelName'])
try:
trim = clean(d['trimName'])
except:
trim = "N/A"
try:
mileage = "{:.2f}".format(float(d['mileage'])/1.609)
except:
mileage = "N/A"
try:
price = clean(str(d['price']))
except:
price = "N/A"
pgvin = rq.get(f"https://www.cargurus.ca/Cars/detailListingJson.action?inventoryListing={d['id']}")
vin = clean(json.loads(pgvin.text)['listing']['vin'])
save_data(f"{year},{make},{model},{trim},{vin},{mileage},{price}")
offset+=15