Python 3.x 如何修复';UnicodeEncodeError:&x27;拉丁语-1';编解码器可以';t将字符编码在位置';在请求中
我正在编写spider,在尝试使用请求时出现了问题 UnicodeEncodeError:“latin-1”编解码器无法对位置中的字符进行编码 如何解决这个问题? 我曾尝试在client.py中将“latin-1”改为“utf-8”,但没有成功 这是windows10,python 3.7Python 3.x 如何修复';UnicodeEncodeError:&x27;拉丁语-1';编解码器可以';t将字符编码在位置';在请求中,python-3.x,python-requests,Python 3.x,Python Requests,我正在编写spider,在尝试使用请求时出现了问题 UnicodeEncodeError:“latin-1”编解码器无法对位置中的字符进行编码 如何解决这个问题? 我曾尝试在client.py中将“latin-1”改为“utf-8”,但没有成功 这是windows10,python 3.7 def get_page(slef): ## slef.url = 'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=
def get_page(slef):
##
slef.url = 'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash={geohash}&latitude={latitude}&limit=24&longitude={longitude}&offset={offset}&terminal=web'.format(
geohash=slef.g, latitude=slef.latitude, offset=slef.limit_num, longitude=slef.longitude)
#
# slef.url = 'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash={geohash}&latitude={latitude}&limit=24&longitude={longitude}&offset={offset}&terminal=web'.format(
# geohash=slef.g, latitude=slef.latitude, offset=slef.limit_num, longitude=slef.longitude)
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36",
"Cookie": "******* 可用开发者工具捕捉也可用 Fiddler获取 ********",
}
t1 = requests.get(slef.url, headers=headers).text
在client.py中
for i, one_value in enumerate(values):
if hasattr(one_value, 'encode'):
values[i] = one_value.encode('latin-1')
elif isinstance(one_value, int):
values[i] = str(one_value).encode('ascii')
if _is_illegal_header_value(values[i]):
raise ValueError('Invalid header value %r' % (values[i],))
请包括完整的错误跟踪我在referer中设置unicode时也有类似问题,我只是在传递到
.get()
(因为我不想更改默认库python3.6/http/client.py“
)之前在try/catch中对unicode进行编码('utf-8'))。