Xploit
Xploit

Reputation: 75

Python : Check an inputs first characters

Okay so i have a problem i looked it up but didnt know exactly what to look up and kept seeing problems that had nothing to do with mine.. So my problem here is im taking an input in python

flashSite = raw_input('[?] Please Provide a Web Url : ')

After it takes the input i want it to check if the http:// characters are included in the beginning of the input , if they are then dont add them in again , if they arent then add them in for the user , Help is greatly appreciated.. Also im new to Stackoverflow so i will have problems with little things like putting code in comments and such:(

edit : So from other answers and comments i came out with this

def ScrapeFlashFiles():
flashSite = raw_input('Please Provide a web URL : ')
if flashSite.lower().startswith(flashSite, beg=0, end=7('http://')):
    return flashSite
elif flashSite.lower().startswith(flashSite, beg=0, end=4('www.')):
    flashSite = 'http://' + flashSite
print ' Sending requests... '
flashReq = requests.get(flashSite)
print ' Scraping content '
flashTree = html.fromstring(flashReq.content)
print 'Searching for keyword \'.swf\' '
for line in flashReq.content.split('\n'):
    if '.swf' in line:
        print line
print 'Flash Scrape Complete..'

Am i doing something wrong here?

Note i am a beginner.. Im getting an error now talking about an int..

Source to where i was reading about the startswith method https://www.tutorialspoint.com/python/string_startswith.htm

Upvotes: 0

Views: 64

Answers (1)

AbrahamB
AbrahamB

Reputation: 1418

raw_input returns a string, as visible from the documentation: https://docs.python.org/2/library/functions.html#

Since you're working with a string Type, you can use any of the string methods https://docs.python.org/2/library/stdtypes.html#string-methods.

For example:

expected_beginning = 'http://'
if not flashSite.startswith(expected_beginning):
  flashSite = expected_beginning + flashSite

You could do interesting things, like make sure it's always lowercase by doing:

if not flashSite.lower().startswith(expected_beginning):

etc.

Upvotes: 1

Related Questions