insert data into a table mysql

Issue

I extract data from truecar.com. This info contains price and miles of different car. Now, I want to insert this information into a table but my code doesn’t work to create a table and return only ‘price’ and ‘miles’ instead of their numbers. could you help me?
Here is my code:

import requests
from bs4 import BeautifulSoup
import mysql.connector
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')

data = []
for card in soup.select('[class="card-content vehicle-card-body order-3 vehicle-card-carousel- 
body"]'):
price = card.select_one('[class="heading-3 margin-y-1 font-weight-bold"]').text

miles = card.select_one('div[class="d-flex w-100 justify-content-between"]').text

data.append({
    'price':price,
    'miles':miles
})
print(data)

cnx = mysql.connector.connect(user='root', password='',
                              host='127.0.0.1',
                              database='truecar')
cursor = cnx.cursor()
for price,miles in data:
     cursor.execute("INSERT INTO car VALUES(\'%s\',\'%s\')"%(price,miles))
     cnx.commit()
cnx.close()

Solution

If you just execute the following code:

price = 10
miles = 20

data = []
data.append({
    'price':price,
    'miles':miles
})

for price, miles in data:
    print(price, miles)

the output on the console will be price miles. So you are not accessing the values of the dictionary but the keys. Therefore you do not write the keys to the database instead of the values. What works is the following code:

price = 10
miles = 20

data = []
data.append({
    'price':price,
    'miles':miles
})
    
for entry in data:
    print(entry["price"], entry["miles"])

I think that Saqibs answer should also be considered because adding the columns of the database to which the data is added will lead to less bugs in more complex software.

Answered By – Lenntror

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published