user4530588
user4530588

Reputation:

Urllib error from python 2 to python 3

im using Python3.4 and i want to use this script. However its made for an earlier version of Python therefore doesn't work. I was hoping if someone can help me change it into python 3 code. Ive tried to import the urllib.request as urllib2 (because apparently urllib2 is merged for python 3)

import re
import sys
import urllib.request as urllib2
from bs4 import BeautifulSoup

usage = "Run the script: ./geolocate.py IPAddress"

if len(sys.argv)!=2:
    print(usage)
    sys.exit(0)

if len(sys.argv) > 1:
    ipaddr = sys.argv[1]

geody = "http://www.geody.com/geoip.php?ip=" + ipaddr
html_page = urllib2.urlopen(geody).read()
soup = BeautifulSoup.BeautifulSoup(html_page)

# Filter paragraph containing geolocation info.
paragraph = soup('p')[3]

# Remove html tags using regex.
geo_txt = re.sub(r'<.*?>', '', str(paragraph))
print geo_txt[32:].strip()

Upvotes: 0

Views: 233

Answers (1)

user1907906
user1907906

Reputation:

print geo_txt[32:].strip()

is Python 2. For Python 3 use

print(geo_txt[32:].strip())

Upvotes: 1

Related Questions