guialmachado
guialmachado

Reputation: 536

Custom For Loop in Python

Is there any pythonic way to do the same as code bellow, but in a pythonic way?

I created this code for web scraping a website, but I think there should be a better way for adding the contents to lists other than repeating the same code for each element.

here are the lists i will add elements to:

Proporcao_de_Sobras = []
liq_dir =[]
liq_sobras=[]
liq_reservas=[]
Encerramento=[]
n_emissao =[]
tp_ofert =[]
inv_minimo =[]

And here is the code I am using to add the elements to lists.

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[2]'):
        Proporcao_de_Sobras.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[4]'):
        liq_dir.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[6]'):
        liq_sobras.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[8]'):
        liq_reservas.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[10]'):
        Encerramento.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[12]'):
        n_emissao.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[14]'):
        tp_ofert.append(x.text)
except:
    pass

try:
    for x in driver.find_elements_by_xpath('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[16]'):
        inv_minimo.append(x.text)
except:
    pass

This goes on for more 5 or 6 times.

Upvotes: 3

Views: 190

Answers (3)

Ivan Litteri
Ivan Litteri

Reputation: 58

Here's another pythonic way using dictionaries:

def get_data(your_lists):
    data = {}
    for list_index, list_name in enumerate(your_lists):
        try:
            data[list_name] = [x for x in find_elements_by_xpath(f'//*[@id="tablepress-6"]/tbody/tr[*]/td/span[{(list_index + 1) * 2}]')]
        except:
            pass
    return data

your_lists = ['Proporcao_de_Sobras', 'liq_dir', 'loq_reservas', 'Encerramento', 'n_emissao', 'tp_ofert', 'inv_minimo']

all_data = get_data(your_lists)

Upvotes: 3

dh762
dh762

Reputation: 2429

You can use a function for it. I would advise to catch specific exceptions though.

def fill_elem(fill_list, xpath):
    try:
        for x in driver.find_elements_by_xpath(xpath)
            fill_list.append(x.text)
    except SomeException:
        pass
    else:
        return fill_list

proporcao_de_sobras = []
proporcao_de_sobras = fill_elem(proporcao_de_sobras, r'//*[@id="tablepress-6"]/tbody/tr[*]/td/span[2]')

Upvotes: 2

Raymond Reddington
Raymond Reddington

Reputation: 1837

Pythonic way N1, using mutability of lists:

def get_text(x_path, dest_list):
    for x in driver.find_elements_by_xpath(x_path):
        dest_list.append(x.text)

Proporcao_de_Sobras = []
get_text('//*[@id="tablepress-6"]/tbody/tr[*]/td/span[2]', Proporcao_de_Sobras)

Pythonic way N2, using dicts:

paths = {
    '//*[@id="tablepress-6"]/tbody/tr[*]/td/span[2]': [],
    '//*[@id="tablepress-6"]/tbody/tr[*]/td/span[4]': [],
    ....
}

for k, v in paths.items():
    for x in driver.find_elements_by_xpath(k):
        v.append(x.text)

Upvotes: 2

Related Questions