在网址中输入中文时出现UnicodeEncodeError

时间:2018-11-27 08:15:44

标签: python

import urllib.request
import bs4
key_word = input('What is the good you are searching for?')
price_low_limit = input('What are the lowest price restrictions?')
price_high_limit = input('What are the highest price restrictions?')
url_jd = 'https://search.jd.com/search?keyword={}&enc=utf-8&qrst=2&rt=1&stop=1&vt=2&wq={}&ev=exprice_{}-{}%5E&uc=0#J_searchWrap'.format(key_word, key_word, price_low_limit, price_high_limit)
response = urllib.request.urlopen(url_jd)
text = response.read().decode()
html = bs4.BeautifulSoup(text, 'html.parser')
total_item_j = []
for information in html.find_all('div', {'class': "gl-i-wrap"}):
    for a in information.find_all('a', limit=1):
        a_title = a['title']
        a_href = a['href']
    for prices in information.find_all('i', limit=1):
        a = prices.text
        item_j = {}
        item_j['price'] = float(a)
        item_j['name'] = a_title
        item_j['url'] = a_href
        total_item_j.append(item_j)
print(total_item_j)

这是我在学校做的一个项目。我想使用此程序提取我搜索的商品价格。目前,此代码可用于python 3.7中的英语输入。但是,如果我搜索中文商品,例如“巧克力”(巧克力),则会发现Unicode编码错误。请帮帮我。

2 个答案:

答案 0 :(得分:0)

您只想确保字符串正确编码。如果您将key_word更改为:

key_word = u'巧克力'.encode('utf-8')

您会发现它工作正常。

所以您的代码如下:

import urllib.request
import bs4
key_word = input('What is the good you are searching for?')
key_word = key_word.encode('utf-8') 
...

有关python here中的unicode的更多信息

答案 1 :(得分:0)

如果查看堆栈跟踪,则会看到类似以下内容的

# Non-ASCII characters should have been eliminated earlier
--> 983         self._output(request.encode('ascii'))

对于key_word变量中的字符,ASCII编码将失败。 应该先将其转义为网址。通过以下方式做到这一点:

key_word = urllib.parse.quote_plus(key_word)

然后准备url_jd字符串。