Home > Net >  How to extract all attribute values of <i> nested in a specific <div>
How to extract all attribute values of <i> nested in a specific <div>

Time:05-12

Im scraping a website where I have a recurring with a varrying number of value.

An example of one div item is like this:

<div >
<i  qtip-tooltip="Français"></i>
<i  qtip-tooltip="Néerlandais"></i>
<i  qtip-tooltip="Anglais"></i>
<i  qtip-tooltip="Italien"></i>
</div>

I would like to get a list of all the gtip-tooltip values in a string or a list for each div. How can I do that?

I tried

langs = driver.find_elements(by=By.XPATH,value='//div[@]') 

but I get empty string values.

CodePudding user response:

You are quite close to your goal - to achieve it simply iterate over the ResultSet of langs, find all <i> elements for each <div> and extract their attributes while iterate again:

langs = driver.find_elements(By.XPATH,'//div[@]')
for lang in langs:
    tooltips = [l.get_attribute('qtip-tooltip') for l in lang.find_elements(By.XPATH,'.//i')]
    
    ## as list
    print(tooltips)
   
    ## as comma separated string
    print(','.join(tooltips))
Output
['Français', 'Néerlandais', 'Anglais', 'Italien']

or

Français,Néerlandais,Anglais,Italien

To get languages over all div as one list or better one set with unique values you could go with:

set(l.get_attribute('qtip-tooltip') for l in driver.find_elements(By.XPATH,'//div[@]/i'))

Output

{'Anglais', 'Français', 'Italien', 'Néerlandais'}

CodePudding user response:

Try :

elements = driver.find_elements_by_css_selector('div.lang') 

Then you can access your elements by loooping on your variable :

For element in elements :
  print(element)

CodePudding user response:

Try:

langs = driver.find_elements(By.XPATH,'//div[@]/i')
for lang in langs:
    lan = lang.get_attribute('qtip-tooltip')
  • Related