相关疑难解决方法(0)

如何在Python中对一个查询字符串进行urlencode?

我在提交之前尝试对此字符串进行urlencode.

queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"]; 
Run Code Online (Sandbox Code Playgroud)

python url-encoding

521
推荐指数
12
解决办法
63万
查看次数

urlib.request.urlopen不接受带空格的查询字符串

我正在python上学习udacity课程,我们应该检查文档中的亵渎词.我正在使用网站http://www.wdylike.appspot.com/?q=(text_to_be_checked_for_profanity).要检查的文本可以作为上述URL中的查询字符串传递,并且在检查亵渎词后网站将返回true或false.以下是我的代码.

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()
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误

回溯(最近一次调用最后一次):文件"/Users/Vrushita/Desktop/Rishit/profanity_check.py",第21行,在read_content()文件"/Users/Vrushita/Desktop/Rishit/profanity_check.py",第11行,在read_content check_profanity(content)文件"/Users/Vrushita/Desktop/Rishit/profanity_check.py",第16行,在check_profanity connection = urllib.request.urlopen(" http://www.wdylike.appspot.com/?q" = "+ text_to_read"文件"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py",第163行,在urlopen中返回opener.open(url,data,timeout)文件"/ Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py",第472行,打开响应= meth(req,response)文件"/Library/Frameworks/Python.framework /Versions/3.5/lib/python3.5/urllib/request.py",第582行,http_response'htt',请求,响应,代码,消息,hdrs)文件"/Library/Frameworks/Python.framework/Versions/ 3.5/lib/python3.5/urllib/request.py",第510行,错误返回self._call_chain(*args)文件"/ Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py",第444行,在_call_chain result = func(*args)文件"/Library/Frameworks/Python.framework/Versions/ 3.5/lib/python3.5/urllib/request.py",第590行,http_error_default引发HTTPError(req.full_url,代码,msg,hdrs,fp)urllib.error.HTTPError:HTTP错误400:错误请求

我试图读取内容的文档包含一个字符串" Hello world "但是,如果我将字符串更改为" Hello + world ",则相同的代码将起作用并返回所需的结果.有人可以解释为什么会发生这种情况以及解决方法是什么?

python python-3.x

2
推荐指数
2
解决办法
1968
查看次数

标签 统计

python ×2

python-3.x ×1

url-encoding ×1