我一直收到来自urlopen的HTTP错误400:错误请求

时间:2017-08-11 11:32:49

标签: python python-3.x

我正在从Udacity学习Python 因为我使用不同的版本,所以我陷入编程亵渎编辑器

这是我的代码:

import urllib.request
def readdocument(x):
    document = open(x)
    profanitycheck(document.read())
    document.close()
def profanitycheck(urcontent):
    q = urllib.request.Request("http://www.wdylike.appspot.com/?q="+urcontent)
    with urllib.request.urlopen(q) as content2:
        output = content2.read()
        print(output)

filelocate=(r"C:\Users\Sutthikiat\Desktop\movie_quotes.txt")   
readdocument(filelocate)

这是txt文件:

-- Houston, we have a problem. (Apollo 13)

-- Mama always said, life is like a box of chocolates. You never know what you are going to get. (Forrest Gump)

-- You cant handle the truth. (A Few Good Men)

-- I believe everything and I believe nothing. (A Shot in the Dark)

但我创建了一个新的文本文件并检查它,它运行正常,所以我不明白我的代码是如何得到错误的,也许是关于异常?

这是错误代码:

Traceback (most recent call last):
  File "C:\Users\Sutthikiat\Desktop\cursecheck.py", line 13, in <module>
readdocument(filelocate)
  File "C:\Users\Sutthikiat\Desktop\cursecheck.py", line 4, in readdocument
profanitycheck(document.read())
  File "C:\Users\Sutthikiat\Desktop\cursecheck.py", line 8, in profanitycheck
with urllib.request.urlopen(q) as content2:
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
  File "C:\Users\Sutthikiat\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

1 个答案:

答案 0 :(得分:2)

服务器认为它无效,就像这样

urllib.request.urlopen("https://www.baidu.com/s?wd="+"a\nb")

url包含invalid character\n(就像您从文件中读取的内容一样)。您需要quote他们:

from urllib.quote import quote
q = urllib.request.urlopen("https://www.baidu.com/s?wd="+ urllib.request.quote("a\nb")) 
print(q.url)
'https://www.baidu.com/s?wd=a%0Ab'