urlretrieve returning typeerror

2019-07-27 21:49发布

问题:

I don't know why my code is returning this error, I can't seem to debug it.

TypeError: expected string or bytes-like object

Here is what I'm using to download

self.headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
self.request = urllib.request.Request(url, headers=self.headers)
urllib.request.urlretrieve(self.request, reporthook=report)

回答1:

It appears that urlretrieve doesn't allow the sending of headers.

And the error you're getting is because urlretrieve is expecting a URL there and not a Request object.

Since it's your own webserver you're sending the request to maybe you can modify its configuration to accept those urlretrieve requests without headers.

Best of luck.