Home > Software design >  scraping data from Truecar.com
scraping data from Truecar.com

Time:08-18

I want to extract the price and the mile of the first 20 cars sold from Truecar.com website but I can not find the suitable tags for this information although tried. My code also must receive the name of the car from users. I can not go forward because cannot find data. Here is my incomplete code:

import requests
from bs4 import BeautifulSoup
car=str(input())
ur='https://www.truecar.com/used-cars-for-sale/listings/'
url=ur car
r=requests.get(url)
soup=BeautifulSoup(r.text,'html.parser')
results=soup.find_all('a',attrs={'class':"linkable order-2 vehicle-card-overlay",'data- 
test':"vehicleCardLink"})

Could you help me?

CodePudding user response:

The following code works. I use CSS Selectors to parse the desired data.

import requests
from bs4 import BeautifulSoup

url='https://www.truecar.com/used-cars-for-sale/listings/'

r=requests.get(url)
soup=BeautifulSoup(r.text,'html.parser')

data = []
for card in soup.select('[]'):
    price = card.select_one('[]').text
 
    miles = card.select_one('div[]').text
    
    data.append({
        'price':price,
        'miles':miles
    })
print(data)

Output:

[{'price': '$74,888', 'miles': '27,423 milesDiscount Available'}, {'price': '$32,999', 'miles': '53,252 miles'}, {'price': '$34,990', 'miles': '65 miles'}, {'price': '$14,445', 'miles': '169,206 milesDiscount Available'}, {'price': 
'$70,788', 'miles': '5,854 miles'}, {'price': '$19,300', 'miles': '21,467 miles'}, {'price': '$9,680', 'miles': '126,555 miles'}, {'price': '$20,499', 'miles': '56,862 miles'}, {'price': '$21,494', 'miles': '50,007 milesUpfront Price Available'}, {'price': '$33,500', 'miles': '43,617 milesUpfront Price Available'}, {'price': '$19,500', 'miles': '38,745 miles'}, {'price': '$11,998', 'miles': '132,495 milesUpfront Price Available'}, {'price': '$18,400', 'miles': '62,088 miles'}, {'price': '$30,683', 'miles': '51,273 milesUpfront Price Available'}, {'price': '$15,000', 'miles': '143,700 miles'}, {'price': '$10,849', 'miles': '85,539 milesUpfront Price Available'}, {'price': '$51,775', 'miles': '15,204 miles'}, {'price': '$12,999', 'miles': '148,605 miles'}, {'price': '$18,836', 'miles': '69,731 miles'}, 
{'price': '$9,990', 'miles': '129,325 miles'}, {'price': '$7,670', 'miles': '64,000 miles'}, {'price': '$20,900', 'miles': '182,907 milesDiscount Available'}, {'price': '$21,900', 'miles': '15,230 miles'}, {'price': '$17,125', 'miles': '141,305 milesDiscount Available'}, {'price': '$22,999', 'miles': '112,615 miles'}, {'price': '$19,995', 'miles': '46,580 milesDiscount Available'}, {'price': '$21,000', 'miles': '17,501 milesDiscount Available'}, {'price': '$9,995', 'miles': '119,941 miles'}, {'price': '$15,241', 'miles': '142,587 miles'}, {'price': '$30,988', 'miles': '59,894 miles'}, {'price': '$15,936', 'miles': '84,249 milesDiscount Available'}, {'price': '$20,990', 'miles': '138,684 
miles'}, {'price': '$25,000', 'miles': '65,626 miles'}]
  • Related