The following works when I paste it on the browser:
But when I try reading the URL with Python nothing happens:
link = "http://www.somesite.com/details.pl?urn=2344" f = urllib.urlopen(link) myfile = f.readline() print myfile
Do I need to encode the URL, or is there something I"m not seeing?
To answer your question:
import urllib link = "http://www.somesite.com/details.pl?urn=2344" f = urllib.urlopen(link) myfile = f.read() print(myfile)
You need to
EDIT (2018-06-25): Since Python 3, the legacy
urllib.urlopen() was replaced by
urllib.request.urlopen() (see notes from https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen for details).
If you"re using Python 3, see answers by Martin Thoma or i.n.n.m within this question: https://stackoverflow.com/a/28040508/158111 (Python 2/3 compat) https://stackoverflow.com/a/45886824/158111 (Python 3)
Or, just get this library here: http://docs.python-requests.org/en/latest/ and seriously use it :)
import requests link = "http://www.somesite.com/details.pl?urn=2344" f = requests.get(link) print(f.text)
Python: - The Bible - 3 Manuscripts in 1 book:
As information protection continues to be a growing concern for businesses today, IT security certifications have become highly desirable, even as the number of certifications has grown. Now you can s...
As the title promises, this book will introduce you to one of the world’s most popular programming languages: Python. It’s aimed at beginning programmers as well as more experienced programmers wh...
Managing and analyzing data have always offered the greatest benefits and the greatest challenges for organizations of all sizes and across all industries. Businesses have long struggled with finding ...