TypeError:request()在urllib3中缺少1个必需的位置参数:“url”

2024-09-27 21:35:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在深入研究Python,并且正在浏览urllib3的文档。尝试运行代码,但似乎没有按预期的方式运行。 我的代码是

import urllib3

t = urllib3.PoolManager
test = t.request('GET', 'https://shadowhosting.net/')
print(test.data)

我得到的错误是

TypeError: request() missing 1 required positional argument: 'url'

我试着换个地方,但还是不起作用。我将遵循文档(用户指南)的开头部分 供参考-https://urllib3.readthedocs.io/en/latest/user-guide.html


Tags: 代码文档httpstestimportdatagetnet
3条回答

位于https://urllib3.readthedocs.io/en/latest/user-guide.html的文件说:

    import urllib3

    http = urllib3.PoolManager()    //You were missing this paranthesis
    r = http.request('GET', 'http://httpbin.org/robots.txt')

或在邮寄要求的情况下

r = http.request('POST','http://httpbin.org/post', fields={'hello': 'world'})

这是一个输入错误,忘记了创建对象的括号:

t = urllib3.PoolManager()

添加它们,它将像魔术一样工作:

import urllib3

t = urllib3.PoolManager()
test = t.request('GET', 'https://shadowhosting.net/')
print(test.data)

如果您想对URL发出GET请求,那么可以使用requests模块

import requests

response = requests.get('https://shadowhosting.net/')
print(response.text)

相关问题 更多 >

    热门问题