-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
31 lines (28 loc) · 1.32 KB
/
main.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
import requests
from bs4 import BeautifulSoup
import sys
import csv
def car_scrape(search_string):
resp = requests.get(search_string)
soup = BeautifulSoup(resp.text, 'lxml')
car_name = soup.find('h1', attrs = {'class': 'col-8 details-title'})
car_price = soup.find('div', attrs = {'class': 'price-value'})
car_power_kw = soup.find('div', attrs = {'id': 'description-power-0'})
car_power_torque = soup.find('div', attrs = {'id': 'description-torque-0'})
car_engine_induction = soup.find('div', attrs = {'id': 'description-induction-0'})
car_engine_cylinders = soup.find('div', attrs = {'id': 'description-cylinders-0'})
car_drive_type = soup.find('div', attrs = {'id': 'description-drive-0'})
with open('index.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([car_name.text.strip(),
car_price.text.strip().replace("*", ""),
car_power_kw.text.strip(),
car_power_torque.text.strip(),
car_engine_induction.text.strip(),
car_engine_cylinders.text.strip(),
car_drive_type.text.strip(),
search_string])
if len(sys.argv) == 1:
car_scrape(input('URL to listing: '))
else:
car_scrape(sys.argv[1])