【问题标题】:Invalid URL error is raised when running urllib review request运行 urllib 审查请求时引发无效 URL 错误
【发布时间】:2019-10-03 18:35:58
【问题描述】:

我的代码如下:

import urllib.request as urllib

def read_text():
    quotes = open(r"C:\Users\hjayasinghe2\Desktop\Hasara Work\Learn\demofile.txt")
    contents_of_file = quotes.read()
    print(contents_of_file)
    quotes.close()
    check_pofanity(contents_of_file)

def check_pofanity(text_to_check):
    connection = urllib.urlopen("http://www.wdyl.com/profanity?q= " + text_to_check)
    output = connection.read()
    print(output)
    connection.close()

read_text()

我得到的错误是:

Traceback (most recent call last):
  File "C:/Users/hjayasinghe2/Desktop/Hasara Work/Learn/check_profanity.py", line 16, in <module>
    read_text()
  File "C:/Users/hjayasinghe2/Desktop/Hasara Work/Learn/check_profanity.py", line 8, in read_text
    check_pofanity(contents_of_file)
  File "C:/Users/hjayasinghe2/Desktop/Hasara Work/Learn/check_profanity.py", line 11, in check_pofanity
    connection = urllib.urlopen("http://www.wdyl.com/profanity?q= " + text_to_check)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1345, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1255, in _send_request
    self.putrequest(method, url, **skips)
  File "C:\Users\hjayasinghe2\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1117, in putrequest
    raise InvalidURL(f"URL can't contain control characters. {url!r} "
http.client.InvalidURL: URL can't contain control characters. '/profanity?q= Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add.' (found at least ' ')

【问题讨论】:

  • 你的网址不能有空格

标签: python python-3.x


【解决方案1】:

您需要对查询进行 URL 编码。尝试类似:

import urllib, urllib.parse
url = "http://www.wdyl.com/profanity?q=" + urllib.parse.quote(text_to_check)
connection = urllib.urlopen(url)

https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote

【讨论】:

  • 得到一个新错误:urllib.error.HTTPError:HTTP 错误 404:未找到
  • en.wikipedia.org/wiki/HTTP_404。 HTTP 404、404 Not Found、404、Page Not Found 或 Server Not Found 错误消息是计算机网络通信中的超文本传输​​协议 (HTTP) 标准响应代码,用于指示浏览器能够与给定服务器通信,但服务器找不到请求的内容。此外,当找到请求的信息但未授予访问权限时,如果服务器也希望不披露此信息,则可能会返回 404 错误。
  • 那么当您使用浏览器访问http://www.wdyl.com/profanity?q=the+man+in+black+fled 时会得到什么?我在这里收到 404 错误。
  • 你是对的,他们已将 URL 更改为“wdylike.appspot.com/?q”。
猜你喜欢
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多