#๐Ÿ”’ scraping data from website but getting image errors?

27 messages ยท Page 1 of 1 (latest)

slender trail
#

Image 1 is the data I am trying to scrape, and image 2 is the error

safe plumeBOT
#

@slender trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slender trail
#
# works on windows 10

# importing neccesary libraries

# "requests" is a python library that is used to send HTTP requests to URLs and retrieve the responses.
# "beautifulsoup" is a Python library that is used for web scraping purposes to pull the data out of HTML and XML files.
# "toastnotifier" is a python library that allows Python programs to display Windows 10 Toast Notifications.

import requests
from bs4 import BeautifulSoup
from win10toast import ToastNotifier

# creating a toastnotifier object

n = ToastNotifier()

# Defining a function for getting data from a url
def getdata(url):
    r = requests.get(url)
    return r.text

#passing my local wheather website into the getdata function
htmldata = getdata("https://forecast.weather.gov/MapClick.php?lat=39.761&lon=-84.1922")


#parsing the html data
soup = BeautifulSoup(htmldata, 'html.parser')


# now time to find the required details and filter them
current_temp = soup.find("p", class_="myforecast-current-lrg")
weather_overview = soup.find("div", class_="col-sm-10 forecast-text")

#filtering to only get the text within the scraped data
current_temp = str(current_temp.get_text())
weather_overview = str(weather_overview.get_text())

#outputting the parsed and filtered data to the windows desktop notifier
n.show_toast("Weather Update",f"The current temperature is {current_temp}", weather_overview,duration = 100000)
#

error^

frozen frost
#

kid

#

@slender trailwhat are u trying to do

#

can u explain more

slender trail
#

okay so I figured it out but I dont understand why it only works when i format it this very specific way

#

whenever I tried to output the data from the variable "weather_overview" I would continually get these weird 'load image' errors

#

but the html code contained only text, so I found the error to be very confusing

#

and I still do!

#

but if I add the variable "weather_overview" in the f-string which is printing the other variable, it works..

#

I dont know why this is, maybe its because when I added another comma, n.show_toast reserves the second space for outputting images rather than text

tender igloo
#

It's a div as far as I can see, not an image

#

no img tag or source or even base64 data

final field
#

looks like the show_toast function is interpreting weather_overview as as if it's supposed to be the icon_path and trying to find an icon located at that path (which doesn't exist)

#

realize that you're sending two separate strings as arguments into show_toast, they don't automatically combine just because you have a comma between them

#

@slender trail

#

This has nothing to do with images in the web page, the web scraping part of this is working 100% fine

#

the problem is that when you send arguments into the function without naming them, they just go in order of how the function was defined.

#

the function show_toast in the library is defined as follows:

def show_toast(self, title="Notification", msg="Here comes the message", icon_path=None, duration=5, threaded=False):
#

so if you pass in 3 unnamed arguments, the first will be the title, the second is the msg, the third is the icon_path. duration was named, so it works properly regardless what order you put it in.

safe plumeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.