Home > Mobile >  Python Requests InvalidURL: Failed to parse URL
Python Requests InvalidURL: Failed to parse URL

Time:02-11

thanks you very much. it has a "\u200b" follow the .com. "\u200b" is an invisable character. the reason is i copy the url from the website.

but something interesting i found: if i use pip install requests==2.21.0(old version), it will not except this err. only use new version it will err.

InvalidURL: Failed to parse URL, some url will except this error while some other url wouldn't.

already upgrade urllib3 and requests

err

urllib3.exceptions.LocationParseError: Failed to parse: https://ssl.bing.com​/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey

requests.exceptions.InvalidURL: Failed to parse: https://ssl.bing.com​/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey

env

Python 3.8.6 (tags/v3.8.6:db45529) [MSC v.1927 64 bit (AMD64)] on win32

PS C:\Windows\system32> python -m pip install --upgrade urllib3
Requirement already satisfied: urllib3 in c:\python38\lib\site-packages (1.26.8)

PS C:\Windows\system32> python -m pip install --upgrade requests
Requirement already satisfied: requests in c:\python38\lib\site-packages (2.27.1)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\python38\lib\site-packages (from requests) (2.0.5)
Requirement already satisfied: idna<4,>=2.5 in c:\python38\lib\site-packages (from requests) (3.2)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\python38\lib\site-packages (from requests) (1.26.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\python38\lib\site-packages (from requests) (2021.5.30)

code

import requests
url='https://ssl.bing.com​/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey'
#res=requests.post(url)
res=requests.post(str(url))

error log

Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\requests\models.py", line 384, in prepare_url
    scheme, auth, host, port, path, query, fragment = parse_url(url)
  File "C:\Python38\lib\site-packages\urllib3\util\url.py", line 394, in parse_url
    return six.raise_from(LocationParseError(source_url), None)
  File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: https://ssl.bing.com​/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/test.py", line 3, in <module>
    res=requests.post(str(url))
  File "C:\Python38\lib\site-packages\requests\api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python38\lib\site-packages\requests\sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "C:\Python38\lib\site-packages\requests\sessions.py", line 443, in prepare_request
    p.prepare(
  File "C:\Python38\lib\site-packages\requests\models.py", line 318, in prepare
    self.prepare_url(url, params)
  File "C:\Python38\lib\site-packages\requests\models.py", line 386, in prepare_url
    raise InvalidURL(*e.args)
requests.exceptions.InvalidURL: Failed to parse: https://ssl.bing.com​/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey

CodePudding user response:

There's a typo, or invalid invisible character in your url. please copy from my code:

url='https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=samplekey'
  • Related