AttributeError: 'module' 对象没有属性 'urlopen' [英] AttributeError: 'module' object has no attribute 'urlopen'

查看:39
本文介绍了AttributeError: 'module' 对象没有属性 'urlopen'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 下载网站的 HTML 源代码,但收到此错误.

I'm trying to use Python to download the HTML source code of a website but I'm receiving this error.

Traceback (most recent call last):  
    File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
     file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'

我正在遵循此处的指南:http://www.boddie.org.英国/python/HTML.html

I'm following the guide here: http://www.boddie.org.uk/python/HTML.html

import urllib

file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()

#I'm guessing this would output the html source code?
print(s)

我使用的是 Python 3.

I'm using Python 3.

推荐答案

这适用于 Python 2.x.

This works in Python 2.x.

对于 Python 3,请查看 文档:

For Python 3 look in the docs:

import urllib.request

with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
    # I'm guessing this would output the html source code ?
    print(s)

这篇关于AttributeError: 'module' 对象没有属性 'urlopen'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆