#๐ please help with traceback error
95 messages ยท Page 1 of 1 (latest)
@hexed torrent
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.
Click here to see this code in our pastebin.
2067 is not the usualy comic. Go to the page, have a look.
Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.
A full traceback could look like:
Traceback (most recent call last):
File "my_file.py", line 5, in <module>
add_three("6")
File "my_file.py", line 2, in add_three
a = num + 3
~~~~^~~
TypeError: can only concatenate str (not "int") to str
If the traceback is long, use our pastebin.
please help with request error
yep @opaque fulcrum
Traceback (most recent call last):
File "/Users/m/Documents/Scripts/wscrape.py", line 20, 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':```
please help with traceback error
It was because it use relative path and omit the domain, which you have to supply I believe
Oh also it wasn't just an image
Maybe that's why
.xhcd 2066
Wait I pretty sure it's a command for it
The CSS should still select the img tag fine. I think it's just that 2067 uses a link relative to the current domain, and other pages use links to a different subdomain.
I see, thanks. It works otherwise just intresting that I got the error
.xkcd 2067
So I would need to implement code to account for the error
Yes, I'd just address the differences as they come up. You may end up wanting to move that URL generating logic to a function.
Yes. If a URL in an href or src lacks a doamin, you're (usually) supposed to use the domain of the page itself.
#sir-lancebot-playground message
Yeah this is just entirely different stuff to handle
As well
Can you provide an example please
And what if the error occurs elsewhere on other pages
And you don't even need web scrapping and should use that imo
https://github.com/python-discord/sir-lancebot/blob/main/bot%2Fexts%2Ffun%2Fxkcd.py#L39-L85
^ check this out
If I'm at web page https://example.com/foo/bah.html and it has a image whose src is /foo.png, you're supposed to get the https://example.com/ from the page's URL. Making https://example.com/foo.png
I'm scraping to save the images to a dir, just practicing using bs4 never used before
You want to use urljoin of the page URL with the image URL to get a full URL. https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin
The same as any time you're getting errors: work through them as you get them. For this error, you could have found out the issue by examining the src attributes in the DevTools. Stuff like that is pretty much required if you're going to do web scraping.
That's honestly one reason why I don't think scraping is a good beginner project. You also need to know basic HTML and CSS, which complicates matters for new programmers.
Or, of course, with a print() call before the failure. And so on. Anyway, the OP needs to look at the values causing the problem.
I mean it managed to work from 3500or so to 2067
ill try that
Just means they all followed some pattern, and 2067 did not. Now you need to see why that page is special.
Right. I'm a web developer so my first instinct in a case like this is to open the DevTools. But ya, anything to examine the data.
I'll inspect it
Thanks
I'm a backend person. My instinct is the terminal.
To the extent that (ignoring javascript) I'm made myself a pilfer dump URL command to dump the page DOM as an indented listing.
I'm glad I have the Dev Tools. Reading HTML from the terminal is painful.
Like this
Click here to see this code in our pastebin.
I noticed that 2067 used canvas instead of img scr
Thus the indented listing above. See what you think.
Kind of reminds me of Pug.
Wot's pug.
A templating language that lets you write HTML using indents like that, and without <>.
I haven't looked through all the pages but it looks like 2067 is the only page to use canvas
I don't think that that's the case. The selector specifies img.
I think the canvas is a red herring.
Well you could just make your code detect lack of an image and skip past it.
right
I see
isn't this illegal (here) or something? you seem to be sending a lot of requests to a website
It uses 2 images thats why - one at the top being img src and the larger being canvas
Although, as well, the images on this page aren't the main focus.
nah
On the scale of things, I doubt it'll even be noticed.
shouldn't be encouraged though...
This is also a project in Automate the Boring Stuff iirc. With as common as that book is, I would expect them to have a TOS discouraging scraping if it bothered them.
I disagree. Scraping should be tolerated unless it unduly loads the site. Eg a massive parallel scrape would be rude
Anyway, this is a site specific terms of service thing. I consider scraping to be an accessibility thing anyway. The scraper's an aid to me reading their site.
And this is their robots.txt:
User-agent: *
Disallow: /personal/
User-agent: GPTBot
Disallow: /
I'm just learning bs4 using this website theres nothing malicious on it - no user data is being collectied - this is only for me personal learning
Which is a whitelist with two exceptions.
Yes
Is the main issue here? comUrl = 'https:' + comElem[0].get('src') in retrieving the src in the img div when the image is in canvas for 2067
The issue is that comElem[0] is not in the form that you're expecting. Like Cameron mentioned, put a print before that line and print it out in the problem case and a good case.
"When in doubt, print it out"
Like - py print(f'Found {len(comElem)} comic images')
The length may be interesting in some cases, but for now, the important part is comElem[0] specifically. I'd do
print(f'Comic img link: {comElem[0]}')
I will partially agree with Sun though that this is potentially hitting their server far more than it needs to. I wouldn't run the entire script until you've worked out the bugs. Hitting up thousands of pages every time you want to do a test is needlessly hitting their servers. It probably doesn't matter since a ton of caching will be in place, but I think it's a good habit to tend to respect other people's servers.
Maybe I should have a limiter to run it up to a certain point then end as this is ongoing it can be much more than 5,000
Yes, and if you're interested in page 2067 specifically right now, you might as well just focus on that page instead of fetching 2000+ pages just to get a link that you already know.
Right
all I would have to do is change comElem = soup.select('#comic img') the img to canvas to make it work for that specific page
right?
I don't think fetching the canvas would help you. The canvas would be drawn at runtime by JS. Since you're using BS4, JS doesn't run, so the canvas will neverbe populated. If you were running JS while scraping, you could export the image from the canvas to get it, but that's not an option with your current set up. The best you can probably do is fetch the two images that are available to you. This special case shows that there may be multiple images per page, so always getting only the 0th img may mean that you're missing other images.
how would I account for both images nested in canvas and img src
The image isn't nested in the canvas:
It's just after the canvas
I haven't used BS4 in forever, but there should be a selectAll or something function that fetches all matches instead of the first.
Wait, nvm, I'm getting tired. select already returns all of them, and you're doing [0] to get the first. Just check all the elements that get found.
This defines the start point - url =
It appears so, yes.
You might find it useful to create a separate "playground" script that's a stripped down version of this one for when you need to examine single pages at a time.
That's what i'm doing right now actually
i see the problem is with the js intergration with the map in zooming in hence the user of canvas I think
!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.