学习Python遇到问题: import urllib.request ImportError: No module named request
程序员文章站
2023-12-29 10:19:40
...
1、最近学习python 写了一个简单的爬虫例子 遇到下面问题 废了我几个小时
# coding=utf-8
import urllib.request
url = "http://www.baidu.com"
data = urllib.request.urlopen(url).read()
data = data.decode('UTF-8')
print(data)
后来发现 我用的是python版本2.7.14
说是源码 urllib moudle中并没有request相关方法,直接是urlopen方法
from urlparse import urljoin as basejoin
__all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve",
"urlcleanup", "quote", "quote_plus", "unquote", "unquote_plus",
"urlencode", "url2pathname", "pathname2url", "splittag",
"localhost", "thishost", "ftperrors", "basejoin", "unwrap",
"splittype", "splithost", "splituser", "splitpasswd", "splitport",
"splitnport", "splitquery", "splitattr", "splitvalue",
"getproxies"]
所以改了一下代码:
# coding=utf-8
import urllib
url = "http://www.baidu.com"
data = urllib.urlopen(url).read()
data = data.decode('UTF-8')
print(data)
立马就解决了
如图: