0

Through web scraping, I get two lists, one is the name and the other is the price of a product. How can I enter these values in two columns at the same time so that the price of each product is in front of it?

import mysql.connector
import re
from bs4 import BeautifulSoup
import requests
list_esm=[]
list_gheymat=[]

cnx = mysql.connector.connect(user='root', password='',
                              host='127.0.0.1',
                              database='vorodi')

cursor = cnx.cursor()



user = str(input())
url =('https://divar.ir/s/tehran?q=')
search = requests.get(url + user)
search.text
soup = BeautifulSoup(search.text, 'html.parser')
esm = soup.find_all('div', attrs={'class':'kt-post-card__title'})
gheymat = soup.find_all('div', attrs={'class':'kt-post-card__top-description'})
for i in esm:
    w = re.sub(r'\s+',' ',i.text).strip()
    list_esm.append(w) 
for i in gheymat:
    q= (re.sub(r'^\s*',' ' ,i.text).strip())
    list_gheymat.append(q)
query_1 = """INSERT INTO divar_2 (name,price) VALUES (%r,%r);""" %(tuple(list_esm),tuple(list_gheymat))
cursor.execute(query_1)
cnx.commit()




cnx.close()

1 Answers1

0

Given that both lists are of the same length you can just iterate over them:

for i in range(len(list_esm)):
    query_1 = """INSERT INTO divar_2 (name,price) VALUES (%r,%r);""" %(list_esm[i], list_gheymat[i])
    print(query_1)

Anna Slastnikova
  • 1,260
  • 9
  • 9
  • 1
    zip would clearly make this for loop unnecessary and you can male a bulk insert – nbk Sep 06 '20 at 14:41