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

问题描述

我正在深入研究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

解决方法

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

t = urllib3.PoolManager()

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

import urllib3

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

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')

或在有POST请求的情况下

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

如果您要向URL发出GET请求,则可以使用requests模块

import requests

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