私は文書内の不公平な単語をチェックするはずのpythonについてudacityコースを取っています。私はウェブサイトhttp://www.wdylike.appspot.com/?q=(text_to_be_checked_for_profanity)を使用しています。チェックされるテキストは、上記のURLのクエリ文字列として渡すことができ、ウェブサイトは不敬な単語をチェックした後、真または偽を返します。以下は私のコードです。urlib.request.urlopenスペースでクエリー文字列を受け入れない
import urllib.request
# Read the content from a document
def read_content():
quotes = open("movie_quotes.txt")
content = quotes.read()
quotes.close()
check_profanity(content)
def check_profanity(text_to_read):
connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+text_to_read)
result = connection.read()
print(result)
connection.close
read_content()
私は "に文字列を変更する場合には、しかし「世界こんにちは」私はからコンテンツを読み込むしようとしていた文書が文字列が含まれている私に、次のエラー
Traceback (most recent call last): File "/Users/Vrushita/Desktop/Rishit/profanity_check.py", line 21, in read_content() File "/Users/Vrushita/Desktop/Rishit/profanity_check.py", line 11, in read_content check_profanity(content) File "/Users/Vrushita/Desktop/Rishit/profanity_check.py", line 16, in check_profanity connection = urllib.request.urlopen(" http://www.wdylike.appspot.com/?q= "+text_to_read) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 163, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 472, in open response = meth(req, response) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 582, in http_response 'http', request, response, code, msg, hdrs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 510, in error return self._call_chain(*args) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 590, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 400: Bad Request
を与えますHello + world "と同じコードが機能し、目的の結果が返されます。誰かがなぜこれが起こっているのか、これに対する回避策は何かを説明することはできますか?
'urllib'はそれを受け入れ、*サーバー*はしていません。スペースは有効なURL文字ではないため、うまくいけません。 –
[URLスペース(%20)をPythonで正式に挿入する方法](http://stackoverflow.com/questions/32762219/how-to-formally-insert-url-space-20-using-python) –
そしてあなたは回避策を与えました: '+'を使用してください。 –