After declaring several empty lists, I want to put proper values in the empty list according to the conditions

Asked 2 years ago, Updated 2 years ago, 73 views

JNA = []
KAL = []
AAR = []
JJA = []
ABL = []
TWB = []
ASV = []

airlines = ['lj-jna', 'ke-kal', 'oz-aar', '7c-jja', 'bx-abl', 'tw-twb', 'rs-asv']

for i in airlines:
    driver.get('https://www.flightradar24.com/data/airlines/%s' % i)
    driver.find_element_by_css_selector('nav > a:nth-child(3)').click()
    driver.find_element_by_css_selector('#list-aircraft > dt:nth-child(2)').click()
    try:
        driver.find_element_by_css_selector('#list-aircraft > dt:nth-child(4)').click()
    except:
        pass
    fleet = driver.find_elements_by_css_selector('td:nth-child(1) > a')
    airline_code = driver.find_element_by_css_selector('div.row.m-t-l.m-l-l > h2').text[3:]
    for j in fleet:
        airline_code.append(j.text)

I'd like to do scraping on a site called

I'd like to put the fleet information in the empty list (JNA, KAL, AAR, etc.) above.

When executing airline_code, "JNA", "KAL", "AAR"... Your back is coming out.

I want to put it in an empty list according to this, but if I do airline_code.append(j.text), the error 'str' object has no attribute 'append' appears. I think the 'JNA' of the list JNA and airline_code are different, so how can I put it on the empty list without labor through the repetition?

python list variable variable-assignment

2022-09-20 19:56

1 Answers

What you want will be dict type. https://wikidocs.net/16043

Please test if the code below works.

scrapped = {}
airlines = ['lj-jna', 'ke-kal', 'oz-aar', '7c-jja', 'bx-abl', 'tw-twb', 'rs-asv']

for i in airlines:
    driver.get('https://www.flightradar24.com/data/airlines/%s' % i)
    driver.find_element_by_css_selector('nav > a:nth-child(3)').click()
    driver.find_element_by_css_selector('#list-aircraft > dt:nth-child(2)').click()
    try:
        driver.find_element_by_css_selector('#list-aircraft > dt:nth-child(4)').click()
    except:
        pass
    fleet = driver.find_elements_by_css_selector('td:nth-child(1) > a')
    airline_code = driver.find_element_by_css_selector('div.row.m-t-l.m-l-l > h2').text[3:]
    for j in fleet:

        # If the airline_code that is not in the scraped dictionary pops out,
        if airline_code not in scrapped :

            # Define a new key by giving an empty list a value
           scrapped[airline_code] = []

        # You can now append the desired value to the array that the airline_code key has
        scrapped[airline_code].append(j.text)

print(scrapped)


2022-09-20 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.