125

The following works when I paste it on the browser:

http://www.somesite.com/details.pl?urn=2344

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?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Helen Neely
  • 4,666
  • 8
  • 40
  • 64

11 Answers11

200

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 read(), not readline()

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)
Asclepius
  • 57,944
  • 17
  • 167
  • 143
woozyking
  • 4,880
  • 1
  • 23
  • 29
  • @KiranSubbaraman it's a really good project, from APIs to the code structure – woozyking Feb 04 '15 at 20:02
  • I also recomends and encourage the programmer to use the new brand `requests` Module, its use yelds to a more Pythonic Code. – Hans Zimermann Jun 01 '17 at 23:41
  • 3
    I am getting the following error on python 3.5.2 :`Traceback (most recent call last): File "/home/lars/parser.py", line 9, in f = urllib.urlopen(link) AttributeError: module 'urllib' has no attribute 'urlopen'` Seems there is no urlopen function in python 3.5. Has it been renamed ? EDIT : Snippet in answer below solves : `from urllib.request import urlopen` – Luatic Jun 25 '18 at 13:44
  • @user7185318 yes in Python 3 the `urlib` package saw some refactoring and API changes. I'll update the answer to emphasize on Python 2. – woozyking Jun 25 '18 at 15:20
  • what if the provided link asks for username and password? How can then the code be changed? – Dr. Essen Sep 17 '19 at 12:12
  • Only the last option using "requests" works properly as others ignore the text encoding. – Shautieh Mar 09 '20 at 11:59
  • do not use this on while loop – greendino Oct 11 '20 at 08:17
45

For python3 users, to save time, use the following code,

from urllib.request import urlopen

link = "https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html"

f = urlopen(link)
myfile = f.read()
print(myfile)

I know there are different threads for error: Name Error: urlopen is not defined, but thought this might save time.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
i.n.n.m
  • 2,936
  • 7
  • 27
  • 51
  • This is not the best way to read data from a url using python3 because it misses out on the benefits of the 'with' statement. See my answer: https://stackoverflow.com/a/56295038/908316 – Freddie Aug 13 '20 at 11:37
  • no this will not work on while loop. one call only. which is suck if you ask me – greendino Oct 11 '20 at 08:18
20

None of these answers are very good for Python 3 (tested on latest version at the time of this post).

This is how you do it...

import urllib.request

try:
   with urllib.request.urlopen('http://www.python.org/') as f:
      print(f.read().decode('utf-8'))
except urllib.error.URLError as e:
   print(e.reason)

The above is for contents that return 'utf-8'. Remove .decode('utf-8') if you want python to "guess the appropriate encoding."

Documentation: https://docs.python.org/3/library/urllib.request.html#module-urllib.request

Freddie
  • 908
  • 1
  • 12
  • 24
12

A solution with works with Python 2.X and Python 3.X makes use of the Python 2 and 3 compatibility library six:

from six.moves.urllib.request import urlopen
link = "http://www.somesite.com/details.pl?urn=2344"
response = urlopen(link)
content = response.read()
print(content)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
1

We can read website html content as below :

from urllib.request import urlopen
response = urlopen('http://google.com/')
html = response.read()
print(html)
Akash Kinwad
  • 704
  • 2
  • 7
  • 22
1
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Works on python 3 and python 2.
# when server knows where the request is coming from.

import sys

if sys.version_info[0] == 3:
    from urllib.request import urlopen
else:
    from urllib import urlopen
with urlopen('https://www.facebook.com/') as \
    url:
    data = url.read()

print data

# When the server does not know where the request is coming from.
# Works on python 3.

import urllib.request

user_agent = \
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'

url = 'https://www.facebook.com/'
headers = {'User-Agent': user_agent}

request = urllib.request.Request(url, None, headers)
response = urllib.request.urlopen(request)
data = response.read()
print data
0
from urllib.request import urlopen

# if has Chinese, apply decode()
html = urlopen("https://blog.csdn.net/qq_39591494/article/details/83934260").read().decode('utf-8')
print(html)
codedge
  • 4,754
  • 2
  • 22
  • 38
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – codedge May 16 '20 at 10:14
0
import requests
from bs4 import BeautifulSoup

link = "https://www.timeshighereducation.com/hub/sinorbis"

res = requests.get(link)
if res.status_code == 200:
    soup = BeautifulSoup(res, 'html.parser')

# get the text content of the webpage
text = soup.get_text()

print(text)

using BeautifulSoup's HTML parser we can extract the content of the webpage.

-1

I used the following code:

import urllib

def read_text():
      quotes = urllib.urlopen("https://s3.amazonaws.com/udacity-hosted-downloads/ud036/movie_quotes.txt")
      contents_file = quotes.read()
      print contents_file

read_text()
ggglni
  • 79
  • 3
-1
# retrieving data from url
# only for python 3

import urllib.request

def main():
  url = "http://docs.python.org"

# retrieving data from URL
  webUrl = urllib.request.urlopen(url)
  print("Result code: " + str(webUrl.getcode()))

# print data from URL 
  print("Returned data: -----------------")
  data = webUrl.read().decode("utf-8")
  print(data)

if __name__ == "__main__":
  main()
ksono
  • 1
-2

The URL should be a string:

import urllib

link = "http://www.somesite.com/details.pl?urn=2344"
f = urllib.urlopen(link)           
myfile = f.readline()  
print myfile
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117