If I use the regex command it comes out as instead of the odds of the betting website. Can someone help me? I am a beginner in python.
Does that page contain what you think it contains? Save it to a file and have a look.
Also, posting a picture makes it a lot more difficult to test it as it’s not possible to copy and paste anything.
And I notice that you’re catching any error that urlopen
might raise, printing a generic error message, but then continuing anyway. That’s not good!
Does that page contain what you think it contains? Save it to a file
and have a look.
I notice that his screenshot shows the printout of the soup, so the OP’s
getting its contents. Whether they’re correct may not be clear.
Also, posting a picture makes it a lot more difficult to test it as it’s not possible to copy and paste anything.
Aye, and it is bad for the visually impaired. And those of us using
email don’t get the image at all, and have to visit the web forum to
look at it.
@Hereforsomehelp, can you copy/paste the text of your environment
instead of grabbing an image?
One thing that is apparent is that there’s lots of JavaScript on that
web page. That might just be UI guff, or it might be integral to the
workings of the page. BeautifulSoup does not run JavaScript.
But I’d debug further before trying more elaborate things.
And I notice that you’re catching any error that
urlopen
might
raise, printing a generic error message, but then continuing anyway.
That’s not good!
What MRAB’s getting at here is this code:
try:
page = urllib.request.urlopen(url)
except:
print("An error occurred")
This catches (a) any error - normally we try to catch only errors we
expect to handle specially and (b) it doesn’t recite the error! So you
don’t know what may have gone wrong.
At the least, you want something like this:
try:
page = urllib.request.urlopen(url)
except Exception as e:
print("An error occurred:", e)
which will at least print out what kind of error it was. But much better
still is to not try/except at all! Then you get the full programme
traceback with the error, which helps with debugging.
What this means is: unless you’ve got some special case in mind you need
to handle, write your code as though it isn’t going to raise an
exception at all!
page = urllib.request.urlopen(url)
Cheers,
Cameron Simpson cs@cskk.id.au