Moh*_*hdi 11 python urllib python-3.x
我想导入urllib以使用函数'request'.但是,我在尝试这样做时遇到了错误.我试过pip install urllib,但仍然有同样的错误.我使用的是Python 3.6.真的很感激任何帮助.
我使用以下代码导入urllib.request:
import urllib.request, urllib.parse, urllib.error
fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand:
words = line.decode().split()
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:ModuleNotFoundError:没有名为'urllib.parse'的模块; 'urllib'不是一个包
urllib是一个标准的Python库(内置),所以你不必安装它。如果您需要使用,只需导入它request:
import urllib.request
Run Code Online (Sandbox Code Playgroud)
如果它不起作用,也许你以错误的方式编译了 python,所以请友善地给我们更多详细信息。
更正后的代码是
import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand:
words = line.decode().split()
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)
Run Code Online (Sandbox Code Playgroud)
运行上面的代码会产生
{'Who': 1, 'is': 1, 'already': 1, 'sick': 1, 'and': 1, 'pale': 1, 'with': 1, 'grief': 1}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67840 次 |
| 最近记录: |