Home > Software design >  Extracting individual strings of an object returned by selenium in python
Extracting individual strings of an object returned by selenium in python

Time:12-30

I am trying to scrape a website using selenium in python where I am finding all that elements that belong to a particular classes 'left' and 'right'.

Selenium returns an object which when printed iteratively prints a series of text.

left = driver.find_elements(by=By.CLASS_NAME, value='left')
right = driver.find_elements(by=By.CLASS_NAME, value='right')

When printing this iteratively, I get the following:

for elem in left:
    print(left.text)

a
b
c

a
b
c
d


a
c
d

And similar set of values for the right object.

I want to create a dictionary with the values from the left as keys and values of right as values in the dictionary (appended as a list to each correspnding unique key.) How could one do this ?

CodePudding user response:

Create an empty dictionary and then use python zip() function to iterate left and right list.

left = driver.find_elements(by=By.CLASS_NAME, value='left')
right = driver.find_elements(by=By.CLASS_NAME, value='right')
result={}
for eleL,eleR in zip(left, right):
   result[eleL.text]=eleR.text
   
print(result)

CodePudding user response:

If you don't want the program to overwrite the key values then use this it will create a list if there are more than one value for the same key.

Try this.

dict_ = {}
for k, v in zip(left, right):
    if k in dict_:
        if isinstance(dict_[k], list):
            dict_[k].append(v)
        else:
            dict_[k] = [dict_[k], v]
    else:
        dict_[k] = v


print(dict_)

Else

Try this.

dict_ = { k : v for k, v in zip(left, right)}
print(dict_)
  • Related