python – urllib“模块对象不可调用”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – urllib“模块对象不可调用”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1771字,纯文字阅读大概需要3分钟。
内容图文
![python – urllib“模块对象不可调用”](/upload/InfoBanner/zyjiaocheng/707/814c7f74bfef4740ba602f501e3c1b7c.jpg)
这是我的第三个python项目,我收到一条错误消息:’module object’不可调用.
我知道这意味着我正在错误地引用变量或函数.但是反复试验并没有帮助我解决这个问题.
import urllib
def get_url(url):
'''get_url accepts a URL string and return the server response code, response headers, and contents of the file'''
req_headers = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13',
'Referer': 'http://python.org'}
#errors here on next line
request = urllib.request(url, headers=req_headers) # create a request object for the URL
opener = urllib.build_opener() # create an opener object
response = opener.open(request) # open a connection and receive the http response headers + contents
code = response.code
headers = response.headers # headers object
contents = response.read() # contents of the URL (HTML, javascript, css, img, etc.)
return code , headers, contents
testURL = get_url('http://www.urlhere.filename.zip')
print ("outputs: %s" % (testURL,))
我一直在使用此链接作为参考:
http://docs.python.org/release/3.0.1/library/urllib.request.html
追溯:
Traceback (most recent call last):
File "C:\Project\LinkCrawl\LinkCrawl.py", line 31, in <module>
testURL = get_url('http://www.urlhere.filename.zip')
File "C:\Project\LinkCrawl\LinkCrawl.py", line 21, in get_url
request = urllib.request(url, headers=req_headers) # create a request object for the URL
TypeError: 'module' object is not callable
解决方法:
在python 3中,urllib.request对象是一个模块.您需要调用此模块中包含的对象.这是Python 2的一个重要更改,如果您使用示例代码,则需要将其考虑在内.
例如,创建Request对象和opener:
request = urllib.request.Request(url, headers=req_headers)
opener = urllib.request.build_opener()
response = opener.open(request)
仔细阅读documentation.
内容总结
以上是互联网集市为您收集整理的python – urllib“模块对象不可调用”全部内容,希望文章能够帮你解决python – urllib“模块对象不可调用”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。