2016-12-18 5 views
0

私は文書内の不公平な単語をチェックするはずの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 "と同じコードが機能し、目的の結果が返されます。誰かがなぜこれが起こっているのか、これに対する回避策は何かを説明することはできますか?

+3

'urllib'はそれを受け入れ、*サーバー*はしていません。スペースは有効なURL文字ではないため、うまくいけません。 –

+0

[URLスペース(%20)をPythonで正式に挿入する方法](http://stackoverflow.com/questions/32762219/how-to-formally-insert-url-space-20-using-python) –

+0

そしてあなたは回避策を与えました: '+'を使用してください。 –

答えて

4

urllibを受理すると、サーバは受け付けません。スペースはvalid URL characterではないので、うまくいけません。

urllib.parse.quote_plus()でクエリ文字列を正しくエスケープします。あなたの文字列がvalid for use in query parametersであることがわかります。またはより良いまだ、すべてのキーと値のペアをエンコードするためにurllib.parse.urlencode() functionを使用します。

from urllib.quote import urlencode 

params = urlencode({'q': text_to_read}) 
connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?" + params) 
2

ザ・を応答の下には、あなたの入力テキスト内のスペースがあるとき3. * 400不正な要求が発生したのpythonのためです。 これを避けるには、解析を使用します。 それをインポートします。

from urllib import request, parse 

URLとともにテキストを送信する場合は、テキストを解析します。

url = "http://www.wdylike.appspot.com/?q=" 
url = url + parse.quote(input_to_check) 

ここでの説明を確認します - https://discussions.udacity.com/t/problem-in-profanity-with-python-3-solved/227328

をUdacity冒涜チェッカープログラム -

from urllib import request, parse 

def read_file(): 
    fhand = open(r"E:\Python_Programming\Udacity\movie_quotes.txt") 
    file_content = fhand.read() 
    #print (file_content) 
    fhand.close() 
    profanity_check(file_content) 

def profanity_check(input_to_check): 
    url = "http://www.wdylike.appspot.com/?q=" 
    url = url + parse.quote(input_to_check) 
    req = request.urlopen(url) 
    answer = req.read() 
    #print(answer) 
    req.close() 

    if b"true" in answer: 
     print ("Profanity Alret!!!") 
    else: 
     print ("Nothing to worry") 


read_file() 
関連する問題