Find only certain elements in table with Beatiful soup

aarribas12

I'm triying to get the href attributes from a table in this web. I have this code to get all of the links but i want to filter so i only access to the href for 'Automaticas' not the 'Manuales'

# Fetch URL
url = 'http://meteo.navarra.es/estaciones/descargardatos.cfm'

request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8')

# Response has UTF-8 charset header, and HTML body which is UTF-8 encoded
response = urllib2.urlopen(request)

# Parse with BeautifulSoup
soup = BeautifulSoup(response,'html.parser')

for a in soup.find_all('a',{'href': re.compile(r'descargardatos_estacion.*')}):
    estacion = 'http://meteo.navarra.es/estaciones/' + a.attrs.get('href')
    print(estacion)
    # descarga_csvs(estacion)

The src above for 'Automaticas' and 'Manuales' are different but i don't know how to filter them.

enter image description here

Wiktor Stribiżew

You can use

for img in soup.find_all(lambda x: x.name == 'img' and 'automatica.gif' in x['src']):
    print(img.next_sibling.next_sibling['href'])

Notes:

  • soup.find_all(lambda x: x.name == 'img' and 'automatica.gif' in x['src']) - fetches all img nodes that contain automatica.gif in the src attribute
  • img.next_sibling.next_sibling['href'] - gets the href value of the second sibling of each found img tag.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

getting next item in beatiful Soup

Encoding problem with Beatiful Soup + Python

Check specific class in Beatiful Soup

Python & Beautiful Soup: Searching only in a certain class

Overflow only certain elements

Problem getting the tags in a specific page using beatiful soup python

How to select HTML element by id that is a number in beatiful soup

`find` only in certain subdirectories

beautiful soup unable to find elements from website

Beautiful-Soup / find element only partly?

MySQL n:m find elements from table one that ONLY have elements from table two

Beautiful Soup only returns last value of table

Beautiful Soup finds only half the wanted table

Python Beautiful Soup cannot find table

Scraping Beautiful Soup - find a value from a table

Beautiful Soup cannot find table on iShares

Find elements up to a certain point

Find elements with certain pattern in attribute

How to get only div with id ending with a certain value in Beautiful Soup?

Beautiful Soup (code was working) now only returns certain tags

Beautiful Soup find first <a> whose title attribute equal a certain string

join with a table but only on certain condition

Use beautiful soup to find elements by textual contents, not text?

Separate different elements after using .find_all in Beautiful Soup

Unable to find elements on Amazon page using beautiful soup 4

SCSS/CSS Using :not selector for only certain elements

Initialize only certain elements of array in dzn file

AngularJS $sce only trust certain elements

Select full tree and only certain child elements