Python Forum
How can I web scrape the "alt" attribute from a "img" tag with Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I web scrape the "alt" attribute from a "img" tag with Python?
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Python Obstacles | Kung-Fu | Full File HTML Document Scrape and Store it in MariaDB BrandonKastning 5 2,926 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  Python Obstacles | American Kenpo | Wiki Scrape URL/Table and Store it in MariaDB BrandonKastning 6 2,866 Dec-29-2021, 12:38 AM
Last Post: BrandonKastning
  Python Obstacles | Karate | HTML/Scrape Specific Tag and Store it in MariaDB BrandonKastning 8 3,183 Nov-22-2021, 01:38 AM
Last Post: BrandonKastning
  Beautifulsoup doesn't scrape page (python 2.7) Hikki 0 2,006 Aug-01-2020, 05:54 PM
Last Post: Hikki
  scrape data 1 go to next page scrape data 2 and so on alkaline3 6 5,215 Mar-13-2020, 07:59 PM
Last Post: alkaline3
  Scrape ASPX data with python... hoff1022 0 4,550 Feb-26-2019, 06:16 PM
Last Post: hoff1022
  [Python / BS4] How to Scrape digitalmatic7 9 9,825 Oct-21-2017, 12:55 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020