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


Messages In This Thread
How can I web scrape the "alt" attribute from a "img" tag with Python? - by cisky - Aug-18-2022, 04:56 PM

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 3,149 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  Python Obstacles | American Kenpo | Wiki Scrape URL/Table and Store it in MariaDB BrandonKastning 6 3,085 Dec-29-2021, 12:38 AM
Last Post: BrandonKastning
  Python Obstacles | Karate | HTML/Scrape Specific Tag and Store it in MariaDB BrandonKastning 8 3,413 Nov-22-2021, 01:38 AM
Last Post: BrandonKastning
  Beautifulsoup doesn't scrape page (python 2.7) Hikki 0 2,099 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,622 Mar-13-2020, 07:59 PM
Last Post: alkaline3
  Scrape ASPX data with python... hoff1022 0 4,629 Feb-26-2019, 06:16 PM
Last Post: hoff1022
  [Python / BS4] How to Scrape digitalmatic7 9 10,205 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