在Python3.6中安装urllib

时间:2017-12-09 15:46:38

标签: python python-3.x urllib

我想导入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) 

但是它给了我这个错误:ModuleNotFoundError:没有名为'urllib.parse'的模块; 'urllib'不是包

here is a screenshot for the error

5 个答案:

答案 0 :(得分:14)

<?php // check if the repeater field has rows of data $i = 1; if( have_rows(‘carousel_images’) ): // loop through the rows use_cases_fields data while ( have_rows(‘carousel_images’) ) : the_row(); $title=get_sub_field(‘image_carousel_discription’); $image = get_sub_field(‘image_carousel1’); if( !empty($image) ): // thumbnail $thumb = $image; ?> <li id=”image11″ data-row=”<?php echo $i; ?>” > <a href=”#home”>“/> <h2 id=”change_h2″ data-row=”<?php echo $i; ?>”><?php echo $title ?></h2> <?php $i++; ?> <?php endif; ?> <?php endwhile; endif; ?> 是标准库,您无需安装它。只需 carousel.on(‘itemSelected.sc’, function (evt) { $(“#change_h2”).each(function(index, elem){ $(“#image11”).each(function(index2, elem2){ var itemH3 = $(elem).attr(‘data-row’); var itemImg = $(elem2).attr(‘data-row’); if (itemH3==itemImg){ var div1 = document.getElementById(“change_h2”); var align = div1.getAttribute(“data-row”); console.log(align); } }); }); });

答案 1 :(得分:3)

更正的代码是

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) 

运行上面的代码会产生

{'Who': 1, 'is': 1, 'already': 1, 'sick': 1, 'and': 1, 'pale': 1, 'with': 1, 'grief': 1}

答案 2 :(得分:1)

urllib是一个标准的python库(内置),因此您无需安装它。如果您需要使用request,请导入它:

import urllib.request

如果它不起作用,你可能会以错误的方式编译python,所以要善待并给我们更多细节。

答案 3 :(得分:0)

之所以发生这种情况,是因为名为urllib.py的本地模块遮盖了您尝试使用的已安装请求模块。当前目录位于sys.path之前,因此本地名称优先于已安装的名称。

出现一个额外的调试技巧是仔细查看Traceback,并意识到所涉及脚本的名称与您要导入的模块匹配。

将文件重命名为url.py之类的文件。 然后,它工作正常。 希望对您有帮助!

答案 4 :(得分:0)

您必须为您的计算机安装32或63位的正确版本,就这样

相关问题