#๐Ÿ”’ img src error

102 messages ยท Page 1 of 1 (latest)

wary shadow
elder prismBOT
#

@wary shadow

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.

elder prismBOT
wary shadow
#

And with ```py
from http.client import InvalidURL
from pyexpat import features
from colorama import Fore

import requests, os, bs4

url = 'https://xkcd.com/2067/'
os.makedirs('xkcd', exist_ok=True)
while not url.endswith('#') :
print(Fore.BLACK + f'\nDownloading page {url}...')

res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
comElem = soup.select('#comic img')

if not comElem:
    print(Fore.RED + 'Could not find comic image.')
else:
    comUrl = 'https:' + comElem[0].get('src')
    print(f'\nDownloading image {comUrl}...')
    res.raise_for_status()

    imageF = open(os.path.join('xkcd', os.path.basename(comUrl)), 'wb')
    for chunk in res.iter_content(10000):
        imageF.write(chunk)
    imageF.close()
    print('Done nga')
    exit()```
#

it prints the text without the hyperlink

wary shadow
#

How do I stop - res = requests.get(comUrl) being ran if the image is not valid

#

I want it to skip this image and continue to the next

topaz cloak
wary shadow
#

as the image uses centre instead of img src

#

on that page

topaz cloak
topaz cloak
wary shadow
#

This is an image scraper, it fails to run 2067

#

I want the script to skip over pages that dont have images

#
Traceback (most recent call last):
  File "/Users/m/Documents/Scripts/scrape test.py", line 21, in <module>
    res = requests.get(comUrl)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/sessions.py", line 575, in request
    prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/sessions.py", line 484, in prepare_request
    p.prepare(
    ~~~~~~~~~^
        method=request.method.upper(),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<10 lines>...
        hooks=merge_hooks(request.hooks, self.hooks),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/models.py", line 367, in prepare
    self.prepare_url(url, params)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/requests/models.py", line 444, in prepare_url
    raise InvalidURL(f"Invalid URL {url!r}: No host supplied")
requests.exceptions.InvalidURL: Invalid URL 'https:/2067/asset/challengers_header.png': No host supplied```
topaz cloak
#

got it

wary shadow
#

yes

topaz cloak
#

the url is invalid

wary shadow
topaz cloak
#

https:/2067 is not a correct url

#

it's this code that is wrong

comUrl = 'https:' + comElem[0].get('src')
topaz cloak
#

not only that, you are just trying to append https: to whatever url you find there

#

that's the real problem

wary shadow
#

nope

#

the problem is with the js in some of the pages

#

I have already tried that

#

changed to http same thing, if u compare 2067 with 2066 you can see the difference if u inspect the image in the middle

#

just wondering how to continue if its not src

topaz cloak
#

oh, the partial url you got on 2067 was for the image above the main content

wary shadow
#

yes but the js interferes with it I guess the module bs4 doesnt handle that

topaz cloak
#

correct, bs4 can't handle js by itself

wary shadow
#

I think my url is fine, if you can suggest a code insert I can try it but I think its the js man

topaz cloak
#

people tend to use something like playwright or selenium for such things

wary shadow
topaz cloak
#

look at the difference between the urls below

https://xkcd.com/2067/asset/challengers_header.png
https:/2067/asset/challengers_header.png
```the first one is the real url and the second one is the one you are trying to get, it's missing the `//xkcd.com` part after `https:`
wary shadow
#

This is the page of the image

topaz cloak
#

the problem with the part under that image that is the main content is that it's not just an image that is loaded by js, it's a whole dynamic

wary shadow
#

yep

topaz cloak
wary shadow
#

how can I skip over it

topaz cloak
#

you can catch the exception and move on

wary shadow
wary shadow
topaz cloak
#

or it's just incomplete

#

this line is missing from the code you posted

res = requests.get(comUrl)
```and is the one the throws that error form line 21 in your file named `/Users/m/Documents/Scripts/scrape test.py`
wary shadow
#

here ```py
from pyexpat import features
from colorama import Fore

import requests, os, bs4

url = 'https://xkcd.com/2067/'
os.makedirs('xkcd', exist_ok=True)
while not url.endswith('#') :
print(Fore.BLACK + f'\nDownloading page {url}...')

res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
comElem = soup.select('#comic img')

if not comElem:
    print(Fore.RED + 'Could not find comic image.')
else:
    comUrl = 'https:' + comElem[0].get('src')
    print(f'\nDownloading image {comUrl}...')
    res = requests.get(comUrl)
    res.raise_for_status()

    imageF = open(os.path.join('xkcd', os.path.basename(comUrl)), 'wb')
    for chunk in res.iter_content(10000):
        imageF.write(chunk)
    imageF.close()
    print('Done nga')
    exit()```
topaz cloak
#

there it is, it's much easier if it's the right code that we are looking at

wary shadow
#

yeah my fault

#

I might look into selenium

topaz cloak
#

instead of

        res = requests.get(comUrl)
```you want
```py
        try:
            res = requests.get(comUrl)
        except requests.exceptions.InvalidURL:
            print("Skipping invalid URL: {comUrl}")
            continue
```or you can use `break` if you want to end the loop instead of `continue`
#

continue runs the next iteration of the loop

#

so it depends on what you want to do

topaz cloak
#

oh, sorry, copied the wrong line

wary shadow
#

you meant - res = requests.get(comUrl)

topaz cloak
#

there, yeah

wary shadow
#

actually worked thanks a lot bro

topaz cloak
#

the text moved up while i was copying it and i didn't check

wary shadow
#

yh, it works but it prints out the print("skipping etc") on every line - i had this issue before with except

#

like it stays on 2067

topaz cloak
#

as i said, the url you construct is not correct

wary shadow
#

so I can't even skip it?

wary shadow
topaz cloak
#

this line

        comUrl = 'https:' + comElem[0].get('src')
```needs to be
```py
        comUrl = 'https://xkcd.com' + comElem[0].get('src')
wary shadow
#

I see

topaz cloak
wary shadow
#

actually works

topaz cloak
#

which is part of the comic for this page, but the main part isn't an image and you can't just download it in the same way

topaz cloak
wary shadow
topaz cloak
wary shadow
#

But the image can be downloaded by itself, js was used for the zooming im feature

#

but there are js games that wouldnt work

topaz cloak
wary shadow
#

Right click and save image as

#

It saves as a png

topaz cloak
#

it just looks like it, it's what the browser has rendered on the canvas that you are saving
if you zoom in on a little part at one side and then save it again you'll only get that as an image

#

this is because it's just the rendered canvas that you are saving as an image

#

it's the javascript and the browser that is doing all the heavy lifting to create that image in your browser

wary shadow
#

I see

#

Thanks

topaz cloak
#

no problem

wary shadow
#

what did u use to learn s a begineer? not just python. Just curious

topaz cloak
#

books ๐Ÿ™‚

wary shadow
#

can you recommend some?

#

any language

topaz cloak
#

not the ones i read back then, i tried to start at age 8 but i couldn't find any good books in my native language, so i hade to wait until i could read at least a bit of english at age 10 when i started to program

topaz cloak
wary shadow
#

!close

elder prismBOT
#
Python help channel closed with !close

This help channel has been closed. 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.