-3

Very new to python, I'm trying to take in a command line argument which is a website and set it to a variable. I use the line below to do this.

sitefile = ur.urlopen(sys.argv[1])

The problem is it only works when formatted perfectly in the command line as 'python filename.py http://mywebsite.com/contacts.html'. I want to be able to drop the http://. I've tried the following 2 ways:

sitefile = ur.urlopen(sys.argv[1].startswith('http://'))

gets error message: AttributeError: 'bool' object has no attribute 'timeout'

sitefile = 'http://' + (ur.urlopen(sys.argv[1]))

gets error message: ValueError: unknown url type: 'mywebsite.com/contacts.html'. It appears to just ignore the first half of this concat.

KM617
  • 137
  • 1
  • 3
  • 16
  • Read the docs, `startswith` doesn't do what you think: `sitefile = ur.urlopen('http://' + sys.argv[1])` – Alexander O'Mara Feb 20 '16 at 18:38
  • `startswith()` returns a bool if the string starts with the arg, so not what you are looking for. You need to add the `'http://` before passing to `ur.urlopen()`. Try `ur.urlopen('http://' + sys.argv[1])` – AChampion Feb 20 '16 at 18:38
  • Thanks you very much! – KM617 Feb 20 '16 at 18:39

1 Answers1

1

What's the ur? give the complete code.

sys.argv[1].startswith('http://') return a bool object, drop the http:// should use sys.argv[1].replace('http://', '').

I think the code should like the following lines:

#!/usr/bin/env python
# encoding: utf-8
import sys
import urllib2

if len(sys.argv) != 2:
    print('usage: {} <url>'.format(sys.argv[0]))
    sys.exit()
url = sys.argv[1]

req = urllib2.urlopen(url)
print(req.read())
Wanming Zhang
  • 323
  • 1
  • 8
  • ur is set to import urllib.request it appears to be a simple concat syntax error as the previous comments solved my issue – KM617 Feb 20 '16 at 18:55