简体   繁体   English

Python urllib'ascii'编解码器无法在位置5编码字符'\\ u2757':序数不在范围内(128)

[英]Python urllib 'ascii' codec can't encode character '\u2757' in position 5: ordinal not in range(128)

Here's my code 这是我的代码

opener = urllib.request.build_opener()
try:
      link = 'https://shopee.com.my/❗-❗-READY-STOCK-❗-❗-UA-UNDER-ARMO-DRAWSTRING-BAG-WATERPROOF-i.48885154.1199018006'
      return opener.open(link).read()
  except Exception as e:
      print('Exception: ' + str(e))
      exit()

I'm trying to read this URL , but then I got the error 我正在尝试阅读此URL ,但后来我收到了错误

Exception: 'ascii' codec can't encode character '\u2757' in position 5: ordinal not in range(128)

Is there any way to read the URL with special character? 有没有办法读取具有特殊字符的URL?

Try this code: 试试这段代码:

# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse


opener = urllib.request.build_opener()
try:
    link = 'https://shopee.com.my/' + urllib.parse.quote_plus('❗-❗-READY-STOCK-❗-❗-UA-UNDER-ARMO-DRAWSTRING-BAG-WATERPROOF-i.48885154.1199018006')
    print(link)
    print(opener.open(link).read())
except Exception as e:
    print('Exception: ' + str(e))
    exit()

It will encode the URL 它将对URL进行编码

https://shopee.com.my/%E2%9D%97-%E2%9D%97-READY-STOCK-%E2%9D%97-%E2%9D%97-UA-UNDER-ARMO-DRAWSTRING-BAG-WATERPROOF-i.48885154.1199018006

but unfortunately still fail since shopee.com.my seems to have an invalid https certificate: 但不幸的是,由于shopee.com.my似乎有一个无效的https证书,所以仍然失败:

Exception: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)>

You should use the requests module, recommended by the official documentation. 您应该使用官方文档推荐的请求模块。 It makes also that easier: 它也更容易:

import requests

url = 'https://shopee.com.my/❗-❗-READY-STOCK-❗-❗-UA-UNDER-ARMO-DRAWSTRING-BAG-WATERPROOF-i.48885154.1199018006'

data = requests.get(url)

print(data.text)

Output: 输出:

<!DOCTYPE html>
<html lang="en">


<head>

    <script>
    // QOS start time must be as early as possible.
    var QOS_PAGE_START_MS = Date.now ? Date.now() : +new Date();
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="google-site-verification" content="mJTGLsUwODg98nXhwcsYGJuVana8TPIz9iUNiniILPM" />
    .....
    .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:UnicodeEncodeError:&#39;ascii&#39; 编解码器无法在位置 0 中对字符 &#39;\Ο&#39; 进行编码:序号不在范围内 (128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character '\u039f' in position 0: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置0编码字符u&#39;\\ xfc&#39;:序数不在范围内(128)-&gt; Excel - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128) -> Excel Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法编码位置78中的字符u&#39;\\ xf1&#39;:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 78: ordinal not in range(128) Python - &#39;ascii&#39; 编解码器无法对位置 5 中的字符 u&#39;\\xe9&#39; 进行编码:序号不在范围内(128) - Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128) python csv unicode'ascii'编解码器无法编码位置1中的字符u'\ xf6':序数不在范围内(128) - python csv unicode 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128) &#39;ascii&#39;编解码器不能对位置186中的字符u&#39;\\ u201d&#39;进行编码:序数不在范围内(128) - 'Ascii' codec can't encode character u'\u201d' in position 186: ordinal not in range(128) &#39;ascii&#39;编解码器无法对位置9中的字符u&#39;\\ u2013&#39;进行编码:序数不在范围内(128) - 'ascii' codec can't encode character u'\u2013' in position 9: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法对位置448中的字符u&#39;\\ u2013&#39;进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 448: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置32编码字符u&#39;\\ u2019&#39;:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 32: ordinal not in range(128) UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\–&#39; in position 3 2: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 3 2: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM