如何使用urllib2?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

如何使用urllib2?

zhbguan
我在python的交互式输入中,输入一下代码
>>>import urllib2
>>>f = urllib2.urlopen('http://www.lemote.com')
>>>print f.read(100)
 
以上可以正常运行。
 
但是,将这几行代码写入到一个文件中,那么运行这个文件,就会出现问题:
 
Traceback (most recent call last):
  File "./log-bbs.py", line 6, in ?
    f = urllib2.urlopen('http//www.lemote.com/')
  File "/usr/lib/python2.4/urllib2.py", line 130, in urlopen
    return _opener.open(url, data)
  File "/usr/lib/python2.4/urllib2.py", line 350, in open
    protocol = req.get_type()
  File "/usr/lib/python2.4/urllib2.py", line 233, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: http//www.lemote.com/
 
请教怎么回事啊?
 
 

zhbguan
2007-01-29

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何使用urllib2?

WiseNeuron
我试验了下,没问题。是你这里  f = urllib2.urlopen('http//www.lemote.com/')
少了个":
",将f = urllib2.urlopen('http//www.lemote.com/') 改为 f = urllib2.urlopen('http://www.lemote.com/')

----- Original Message -----
Sent: Monday, January 29, 2007 10:15 PM
Subject: [python-chinese] 如何使用urllib2?

我在python的交互式输入中,输入一下代码
>>>import urllib2
>>>f = urllib2.urlopen('http://www.lemote.com')
>>>print f.read(100)
 
以上可以正常运行。
 
但是,将这几行代码写入到一个文件中,那么运行这个文件,就会出现问题:
 
Traceback (most recent call last):
  File "./log-bbs.py", line 6, in ?
    f = urllib2.urlopen('http//www.lemote.com/')
  File "/usr/lib/python2.4/urllib2.py", line 130, in urlopen
    return _opener.open(url, data)
  File "/usr/lib/python2.4/urllib2.py", line 350, in open
    protocol = req.get_type()
  File "/usr/lib/python2.4/urllib2.py", line 233, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: http//www.lemote.com/
 
请教怎么回事啊?
 
 

zhbguan
2007-01-29


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese