Python Forum

Full Version: How can I web scrape the "alt" attribute from a "img" tag with Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to web scrape the attribute "alt" from a "img" tag of this website: https://www.meteo.it/meteo/roma-oggi-58091. As you may understand, even if the website is in Italian, it's a weather forecast website. The image I'm trying to scrape the "alt" attribute from is the first image: the one that shows the weather at current time (next to the writing "ore" + the hour). I need to webscrape the "alt" attribute because it contains the written infos about the current weather. How should I do that? I'm using Beautiful Soup.


This is the code I've written, but it seems to find no "alt" attribute. In fact, the output is just "[ ]". If I print the variable "img_weather", on the other hand, it prints various "img" tags which do have the attribute I'm looking for.

    from cmath import inf
    import bs4, requests, webbrowser

    LINK = "https://www.meteo.it/meteo/roma-oggi-58091"

    response = requests.get(LINK)
    response.raise_for_status()

    soup = bs4.BeautifulSoup(response.text, 'html.parser')

    img_weather = soup.findAll('img')

    alt_weather = []

    for img in img_weather:
        alt = str(img.find('img'))
        if alt != 'None':
            alt_weather.append(alt)

    print(alt_weather)
Thanks in advance.
Images(svg) are just use as backgrounds,need to parse the values needed.
A example.
import bs4, requests, webbrowser

LINK = "https://www.meteo.it/meteo/roma-oggi-58091"
response = requests.get(LINK)
response.raise_for_status()
soup = bs4.BeautifulSoup(response.content, 'html.parser')
# can just copy CSS selector from browser(inspect Dev Tools)
weather_info = soup.select_one('#main-locations > nav > div > div:nth-child(1)')  
>>> weather_info.text
'Roma27°'
>>> weather_info.p.text
'Roma'
>>> weather_info.p.next_sibling.text
'27°'
>>> # The image used as background for tag over
>>> weather_info.img.get('src')
'//w-static.meteosuper.it/public/icons/ic_cloudy_day_100px_10_26.svg