[Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1

Richard D. Moores rdmoores at gmail.com
Mon Oct 25 18:19:38 CEST 2010


On Mon, Oct 25, 2010 at 08:38, Sander Sweers <sander.sweers at gmail.com> wrote:

> Have you actually tried reading the documentation?

Of course. Though I can see why you wondered.

>The _very_ first
> section of the urllib documentation we have urllib.request.urlopen
> [1]. Which looks to me what you are looking for as a replacement to
> urllib2.urlopen().

What I tried were these 3 lines:

import urllib
a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500)
print(a[123:140])

which got me

Traceback (most recent call last):
  File "c:\P31Working\test_urllib.py", line 2, in <module>
    a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500)
AttributeError: 'module' object has no attribute 'request'
Process terminated with an exit code of 1

Doing it your way,

from urllib import request
a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500)
print(a[123:140])

succeeds. Why?

Anyway, thanks.

Dick


More information about the Tutor mailing list