I have a web scraping python script that when you run , it asks for a web address. What I want to happen is to validate the users input eg. if it's a valid web address or when there is no input from the user. I have done the try and except which almost works, it displays the message that I want the user to see but it also returns Traceback calls and I dont want that. I only want to display my custom error message. Could anyone help me to implement this? Here's my code:
import sys, urllib, urllib2
try:
url= raw_input('Please input address: ')
webpage=urllib.urlopen(url)
print 'Web address is valid'
except:
print 'No input or wrong url format usage: http://wwww.domainname.com '
def wget(webpage):
print '[*] Fetching webpage...\n'
page = webpage.read()
return page
def main():
sys.argv.append(webpage)
if len(sys.argv) != 2:
print '[-] Usage: webpage_get URL'
return
print wget(sys.argv[1])
if __name__ == '__main__':
main()
You can simply do:
try:
# ...
except Exception as e:
print("What you want to show")
Edit: "How do I stop it from executing when it reach an exception?"
You can either have try and except in wget() as #sabujhassan mentioned or you can exit on catching the exception:
except Exception as e:
print("Exception caught!")
exit(1)
Edit 2: "is it possible to loop the program eg. when there is no user input, just keep asking the user to input a web address?"
Yes, You can simply cover it under infinite while loop and break when the right value is selected.
while True:
try:
# Your logic ...
break
except:
print 'No input or wrong url format usage: http://wwww.domainname.com '
print 'Try again!'
use try except for both the function wget() and main(). for example:
def wget(webpage):
try:
print '[*] Fetching webpage...\n'
page = webpage.read()
return page
except:
print "exception!"
You perform the initial try/except, but you're not exiting once the exception is caught. The problem is the webpage will only be filled in when something is passed in, so it fails later since "webpage" has not been defined yet, so the answer is to quit once the exception is thrown.
So:
try:
url= raw_input('Please input address: ')
webpage=urllib.urlopen(url)
print 'Web address is valid'
except:
print 'No input or wrong url format usage: http://wwww.domainname.com '
sys.exit(1)
Try this (replaces lines 3-8) :
def main(url = None) :
if not url : # no arguments through sys.argv
url= raw_input('Please input address: ')
if not url : # No arguments from the user
try :
webpage=urllib.urlopen(url)
except : # Funky arguments from the user
print 'Web address is valid'
finally :
wget(webpage)
else :
print 'No input or wrong url format usage: http://wwww.domainname.com '
For the latter half, (from main onwards) :
if __name__ == '__main__':
if len(sys.argv) == 2 :
main(sys.argv[1])
else :
main()
(I disapprove of the pythonic 4 spaces rule, I keep on having to replace spacebars)
Related
I use the SIMBAD service a lot for looking up the coordinates of celestial objects. I thought I'd automate this using python. My first approach was using Selenium and having it run in Safari. However, this proved to be somewhat harder than doing it manually. About two days ago however I found out about astroquery and how it has SIMBAD built into it. So I thought I'd use that. It does work, but it keeps raising this Attribute Error and all of my attempts to fix it have resulted in a new bug. Here's the code: ( p.s I am using the astroquery library (( astroquery.simbad )) and a text formatter, rich ) :
from rich import print
from astroquery.simbad import Simbad
def ask_for(prompt, error_msg=None, _type=None):
""" While the desired prompt is not given, it repeats the prompt. """
while True:
inp = input(prompt).strip()
if not inp:
if error_msg:
print(error_msg)
continue
if _type:
try:
inp = _type(inp)
except ValueError:
if error_msg:
print(error_msg)
continue
return inp
def simbad_query():
not_target = True
while not_target:
try:
print(
'\nTo find the [blue]RA and Dec[/blue] of your target, please put it in here.')
print("If your target can't be found, it will automatically redirect you to the website to put it in again.")
target = ask_for('\nTarget name: ')
query = Simbad.query_object(f'{target}')
query.pprint()
except AttributeError:
print(
'\nThe target you gave was [red]not found[/red]. Please try again.')
print(
'\nTo find the [blue]RA and Dec[/blue] of your target, please put it in here.')
print("If your target can't be found, it will automatically redirect you to the website to put it in again.")
target = ask_for('\nTarget name: ')
query = Simbad.query_object(f'{target}')
query.pprint()
I am facing this problem while I try to loop tweet_id using the API and write it to tweet_json.txt, the output for all data is Failed which I know is wrong
Before it was working good but when I try to Run all the code again it starts to show failed
for tweet_id in df['tweet_id']:
try:
tweet = api.get_status(tweet_id, tweet_mode = 'extended')
with open('tweet_json.txt', 'a+') as file:
json.dump(tweet._json, file)
file.write('\n')
print (tweet_id, 'success')
except:
print (tweet_id, 'Failed')
Your except is swallowing whatever exception is causing your code to die. Until you comment out the except or make it more specific you won't know if your problem is the Twitter API or file I/O or something else. Good luck!
A quick step forward would be to adjust your exception handler so that it writes the exception. I like to use the format_exc function to get my stack traces so i can write it with a logger, or however i want to handle it.
from traceback import format_exc
try:
a = "" + 1
except Exception as ex:
print("Exception encountered! \n %s " % format_exc())
I'm writing a script that takes in user input at multiple points. If the input is correct, all I need is to break out of the loop and continue. But if it's wrong, what I'm trying to do is delete and then re-prompt for that input. Here is a general case of what I'm working towards:
while True:
test = input("Enter test to check: ")
if test == expected:
break
else:
print("Error: test is incorrect")
#Clear/overwrite test
#Re-prompt for input
I've been doing some research but I haven't found anything that really makes sense to me. Is there a way to delete or overwrite the value entered by the user so that once test is entered again, it's an entirely new value?
If that's very important to you to clear the screen before asking user again for input you could wait some time after displaying error message and then ask user for input again wiping out error message.
import time
expected = 'foo'
while True:
print(' '*30 + '\r', end='')
test = input("Enter test to check: ")
if test == expected:
break
else:
print("Error: {} is incorrect\r".format(test), end='')
time.sleep(2)
If you would like to clear error message and previous input you could use for example os.system('clear').
solution:
else:
print("Error: test is incorrect")
test = input("Enter test to check")
You don't have to 'clear' test as you're reprompting and therefore changing the value of test each time
Why not do something like this
lol=True
while lol:
test = input("Enter test to check: ")
if test == expected:
lol=False
print("Corect!!!")
else:
print("Error: test is incorrect")
Hope it helps
I have a problem with a program I am making to do with an arithmetic quiz.when I enter a name the quiz should print the welcome message however if I enter a blank or a number then the programme should loop the question but tell the user that they have made an error. I used the try statement and the NameError statement to try and resolve this problem but when I use the function "except" and run it in idle it says that I have an invalid syntax.
here is the code i am trying to fix:
import random
while True:
try:
UserName = input("What is your name:")
except NameError:
print ("The answer given does not compute!! Try again")
continue
else:
break
print(UserName," welcome to the Arithmetic Quiz.")
I have edited the code for the program, but still when I try to run it in Idle it highlights except and then says invalid syntax.
You don't need a try-except to check for an empty string
while True:
UserName = input("What is your name:")
if not UserName:
print("The answer given does not compute!! Try again")
continue
else:
break
print(UserName," welcome to the Arithmetic Quiz.")
This is what the proper indentation should be for the try: except: else: block
import random
while True:
try:
UserName = input("What is your name:")
except NameError:
print("The answer given does not compute!! Try again")
continue
else:
break
print(UserName," welcome to the Arithmetic Quiz.")
caveat (thanks cricket_007) this loop will never end. I had meant this comment to show how the try: indentation should have been for the OP.
Hi How to identify more than one value in the particular method.For example my method will take two values to send some balance(eg: amount and account number). I did for one value and I am getting errors if method has more than one value
url='http://www.testfire.net/bank/ws.asmx?WSDL'
client = Client(url)
print client
for method in client.wsdl.services[0].ports[0].methods.values():
print "the existing methods in webservice are:" +method.name
while True:
try:
s = raw_input("Enter the name of the method you want to scan: ")
name= getattr(client.service,s)
break
except suds.MethodNotFound as e:
print "Please enter a valid method."
value=raw_input("enter a value for method: ")
result=name(value)
print result
Pls provide suggestions