'HTTPResponse' object has no attribute 'type' when trying to catch 404 errors












1















I have some code that pulls incomplete URLs from an API and appends these to a base URL. I am trying to extend this to test each URL to make sure it does not lead to a 404 before printing out to screen.



I looked over other answers on how to use urllib with python3 and thought I had done everything correctly, however, I am getting the error in the title.



testurl is my request and resp is my response. This is the code I am using:



                testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")


What am I missing?



The full error output:



Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'


edit:



After the problem is pointed out due to Bruno's answer, I try the following code instead:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


However, this results in nothing being printed at all.










share|improve this question

























  • Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

    – ParvBanks
    Jan 2 at 9:05











  • @Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

    – Jake Rankin
    Jan 2 at 9:09











  • Possibly duplicate with stackoverflow.com/questions/18070600/…

    – duong_dajgja
    Jan 2 at 9:23











  • @duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

    – Jake Rankin
    Jan 2 at 9:25
















1















I have some code that pulls incomplete URLs from an API and appends these to a base URL. I am trying to extend this to test each URL to make sure it does not lead to a 404 before printing out to screen.



I looked over other answers on how to use urllib with python3 and thought I had done everything correctly, however, I am getting the error in the title.



testurl is my request and resp is my response. This is the code I am using:



                testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")


What am I missing?



The full error output:



Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'


edit:



After the problem is pointed out due to Bruno's answer, I try the following code instead:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


However, this results in nothing being printed at all.










share|improve this question

























  • Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

    – ParvBanks
    Jan 2 at 9:05











  • @Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

    – Jake Rankin
    Jan 2 at 9:09











  • Possibly duplicate with stackoverflow.com/questions/18070600/…

    – duong_dajgja
    Jan 2 at 9:23











  • @duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

    – Jake Rankin
    Jan 2 at 9:25














1












1








1








I have some code that pulls incomplete URLs from an API and appends these to a base URL. I am trying to extend this to test each URL to make sure it does not lead to a 404 before printing out to screen.



I looked over other answers on how to use urllib with python3 and thought I had done everything correctly, however, I am getting the error in the title.



testurl is my request and resp is my response. This is the code I am using:



                testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")


What am I missing?



The full error output:



Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'


edit:



After the problem is pointed out due to Bruno's answer, I try the following code instead:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


However, this results in nothing being printed at all.










share|improve this question
















I have some code that pulls incomplete URLs from an API and appends these to a base URL. I am trying to extend this to test each URL to make sure it does not lead to a 404 before printing out to screen.



I looked over other answers on how to use urllib with python3 and thought I had done everything correctly, however, I am getting the error in the title.



testurl is my request and resp is my response. This is the code I am using:



                testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")


What am I missing?



The full error output:



Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'


edit:



After the problem is pointed out due to Bruno's answer, I try the following code instead:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


However, this results in nothing being printed at all.







python-3.x urllib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:01







Jake Rankin

















asked Jan 2 at 7:54









Jake RankinJake Rankin

16210




16210













  • Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

    – ParvBanks
    Jan 2 at 9:05











  • @Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

    – Jake Rankin
    Jan 2 at 9:09











  • Possibly duplicate with stackoverflow.com/questions/18070600/…

    – duong_dajgja
    Jan 2 at 9:23











  • @duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

    – Jake Rankin
    Jan 2 at 9:25



















  • Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

    – ParvBanks
    Jan 2 at 9:05











  • @Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

    – Jake Rankin
    Jan 2 at 9:09











  • Possibly duplicate with stackoverflow.com/questions/18070600/…

    – duong_dajgja
    Jan 2 at 9:23











  • @duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

    – Jake Rankin
    Jan 2 at 9:25

















Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

– ParvBanks
Jan 2 at 9:05





Can you explain what's the error you're getting with this code? Also, where is the urljoin(base_url, modurl) function defined?

– ParvBanks
Jan 2 at 9:05













@Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

– Jake Rankin
Jan 2 at 9:09





@Parvbanks I didn't think the urljoin code was relevant, as it is tested and working, trying to only put the relevant code in question. The error is as per the title, I will edit the question to show the exact output

– Jake Rankin
Jan 2 at 9:09













Possibly duplicate with stackoverflow.com/questions/18070600/…

– duong_dajgja
Jan 2 at 9:23





Possibly duplicate with stackoverflow.com/questions/18070600/…

– duong_dajgja
Jan 2 at 9:23













@duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

– Jake Rankin
Jan 2 at 9:25





@duong_dajgja the cause of the error in the question you linked is not the cause of the problem in this question here. The error is the same but the question is not at all a dupe.

– Jake Rankin
Jan 2 at 9:25












1 Answer
1






active

oldest

votes


















0














Here:



 testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)


The first line calls urlopen and binds the result (an HTTPResponse object) to testurl. Then in the try block, you call urlopen a second time, with the HTTPResponse object as argument - which is, of course, invalid.



EDIT:



with your edited code, ie:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


"it worked" will only get printed if an HTTPError is raised AND it's not a 404 - the else clause matches the if e.code == 404. So of course if there's no error then nothing get printed at all.



What you want is along the line of:



try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)


So in your case, it would looks like:



try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))


Note that here the else clause matches the try clause.






share|improve this answer


























  • I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

    – Jake Rankin
    Jan 2 at 9:31











  • @JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

    – bruno desthuilliers
    Jan 2 at 9:52











  • my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

    – Jake Rankin
    Jan 2 at 10:00











  • The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

    – bruno desthuilliers
    Jan 2 at 12:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002959%2fhttpresponse-object-has-no-attribute-type-when-trying-to-catch-404-errors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here:



 testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)


The first line calls urlopen and binds the result (an HTTPResponse object) to testurl. Then in the try block, you call urlopen a second time, with the HTTPResponse object as argument - which is, of course, invalid.



EDIT:



with your edited code, ie:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


"it worked" will only get printed if an HTTPError is raised AND it's not a 404 - the else clause matches the if e.code == 404. So of course if there's no error then nothing get printed at all.



What you want is along the line of:



try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)


So in your case, it would looks like:



try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))


Note that here the else clause matches the try clause.






share|improve this answer


























  • I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

    – Jake Rankin
    Jan 2 at 9:31











  • @JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

    – bruno desthuilliers
    Jan 2 at 9:52











  • my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

    – Jake Rankin
    Jan 2 at 10:00











  • The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

    – bruno desthuilliers
    Jan 2 at 12:54
















0














Here:



 testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)


The first line calls urlopen and binds the result (an HTTPResponse object) to testurl. Then in the try block, you call urlopen a second time, with the HTTPResponse object as argument - which is, of course, invalid.



EDIT:



with your edited code, ie:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


"it worked" will only get printed if an HTTPError is raised AND it's not a 404 - the else clause matches the if e.code == 404. So of course if there's no error then nothing get printed at all.



What you want is along the line of:



try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)


So in your case, it would looks like:



try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))


Note that here the else clause matches the try clause.






share|improve this answer


























  • I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

    – Jake Rankin
    Jan 2 at 9:31











  • @JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

    – bruno desthuilliers
    Jan 2 at 9:52











  • my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

    – Jake Rankin
    Jan 2 at 10:00











  • The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

    – bruno desthuilliers
    Jan 2 at 12:54














0












0








0







Here:



 testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)


The first line calls urlopen and binds the result (an HTTPResponse object) to testurl. Then in the try block, you call urlopen a second time, with the HTTPResponse object as argument - which is, of course, invalid.



EDIT:



with your edited code, ie:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


"it worked" will only get printed if an HTTPError is raised AND it's not a 404 - the else clause matches the if e.code == 404. So of course if there's no error then nothing get printed at all.



What you want is along the line of:



try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)


So in your case, it would looks like:



try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))


Note that here the else clause matches the try clause.






share|improve this answer















Here:



 testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)


The first line calls urlopen and binds the result (an HTTPResponse object) to testurl. Then in the try block, you call urlopen a second time, with the HTTPResponse object as argument - which is, of course, invalid.



EDIT:



with your edited code, ie:



try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")


"it worked" will only get printed if an HTTPError is raised AND it's not a 404 - the else clause matches the if e.code == 404. So of course if there's no error then nothing get printed at all.



What you want is along the line of:



try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)


So in your case, it would looks like:



try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))


Note that here the else clause matches the try clause.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 12:41

























answered Jan 2 at 9:28









bruno desthuilliersbruno desthuilliers

51.6k54465




51.6k54465













  • I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

    – Jake Rankin
    Jan 2 at 9:31











  • @JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

    – bruno desthuilliers
    Jan 2 at 9:52











  • my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

    – Jake Rankin
    Jan 2 at 10:00











  • The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

    – bruno desthuilliers
    Jan 2 at 12:54



















  • I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

    – Jake Rankin
    Jan 2 at 9:31











  • @JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

    – bruno desthuilliers
    Jan 2 at 9:52











  • my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

    – Jake Rankin
    Jan 2 at 10:00











  • The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

    – bruno desthuilliers
    Jan 2 at 12:54

















I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

– Jake Rankin
Jan 2 at 9:31





I based my code here on the accepted answer to this quetsion: stackoverflow.com/questions/1726402/… - If what I am doing is incorrect why would it be the accepted answer to another question?

– Jake Rankin
Jan 2 at 9:31













@JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

– bruno desthuilliers
Jan 2 at 9:52





@JakeRankin Did you really bothered reading my answer at all ??? FWIW, in the accepted answer of the post you link to, no one is passing an HTTPResponse to urlopen - which doesn't make any sense, quite obviously.

– bruno desthuilliers
Jan 2 at 9:52













my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

– Jake Rankin
Jan 2 at 10:00





my apologies. If I remove the first line assigning testurl, and give a url as a paramter to urlopen being assigned to resp, e.g. resp = urllib.request.urlopen("http://www.google.com"), nothing is printed. If that is not the correct approach, what is?

– Jake Rankin
Jan 2 at 10:00













The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

– bruno desthuilliers
Jan 2 at 12:54





The "correct approach" is to try to understand what you're doing actually. wrt/ your second code snippet, the reason why "nothing is printed" is absolutely obvious, cf my edited answer.

– bruno desthuilliers
Jan 2 at 12:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002959%2fhttpresponse-object-has-no-attribute-type-when-trying-to-catch-404-errors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules