0

I have written a scraper using selenium which has one part very unoptimised. I am assigning values to variables in iterations. I am sure there is a way to do it in one iteration only. I just do not know how. Here is the code I am using:

params = driver.find_elements_by_class_name('param-label')
values = driver.find_elements_by_class_name('param-value')

i = 0

for param, value in zip(params, values):

if not done:

    if param.text == 'Celková cena:':
        cena = value.text
    if param.text == 'Poznámka k ceně:':
        poznamkaCena = value.text
    if param.text == 'Aktualizace:':
        aktualizace = value.text
    if param.text == 'Stavba:':
        stavba = value.text
    if param.text == 'Stav objektu:':
        stavObjektu = value.text
    if param.text == 'Vlastnictví:':
        vlastnictvi = value.text
    if param.text == 'Umístění objektu:':
        umisteniObjektu = value.text
    if param.text == 'Podlaží:':
        podlazi = value.text
    if param.text == 'Užitná plocha:':
        uzitnaPlocha = value.text
    if param.text == 'Sklep:':
        sklep = value.text
    if param.text == 'Voda:':
        voda = value.text
    if param.text == 'Plyn:':
        plyn = value.text
    if param.text == 'Odpad:':
        odpad = value.text
    if param.text == 'Elektřina:':
        elektrina = value.text
    if param.text == 'Doprava:':
        doprava = value.text
    if param.text == 'Energetická náročnost budovy:':
        energetickaNarocnost = value.text

i += 1

if i == len(params):
    done = True

Please, could you tell me how to do this in one iteration?

Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
Dominik Novotný
  • 336
  • 1
  • 16
  • 4
    Why do you need individual variables? Can you explain what your code ultimately does? – OneCricketeer Jan 01 '20 at 11:53
  • So you need to check if `param.text == text` and then save to different variable each time? – Marios Nikolaou Jan 01 '20 at 11:54
  • 1
    (1) You can convert all the subsequent `if` to `elif`. (2) No need to check for `done==True` and `i+=1`, `zip` by default stops iterating when shorter list exhausts. If you want `zip` to iterate till longer list exhausts, use this: https://stackoverflow.com/a/1277311/6400614 – Rahul Bharadwaj Jan 01 '20 at 11:56
  • Can you use a dictionary? `{params[i].text : values[i].text for i in range(len(params))}` – OneCricketeer Jan 01 '20 at 11:57
  • The code gets parameters and values from a reality webiste. Both params and values are a list. I then send these values using variables into a sqlite database. I have these values on a page load and I am sure I can store them into variables in one iteration only. I just do not know how to reference these values from the list. – Dominik Novotný Jan 01 '20 at 12:09
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Georgy Jan 01 '20 at 12:27

2 Answers2

3

You don't need all the if and many variables, use dicts instead. You also don't need to use the done flag, zip will do it for you

variables = {'cena': '', 'poznamkaCena': '', 'aktualizace': ''}
texts = {'Celková cena:': 'cena', 'Poznámka k ceně:': 'poznamkaCena', 'Aktualizace:': 'aktualizace'}

params = driver.find_elements_by_class_name('param-label')
values = driver.find_elements_by_class_name('param-value')

for param, value in zip(params, values):
    if param.text in texts:
        variables[texts[param.text]] = value.text

variables['cena'] will have the same value as cena variable.

Guy
  • 46,488
  • 10
  • 44
  • 88
0
your_dict = {p: v for p, v in zip(params, values) if p in [x for x in texts if x in params]}
mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19