Python urllib 'ascii' 编解码器无法对位置 5 中的字符 '\u2757' 进行编码:序号不在范围内 (128)

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

这是我的代码

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()

我正在尝试读取 this URL,但随后出现错误

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

有什么方法可以读出带有特殊字符的URL吗?

试试这个代码:

# -*- 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()

它将编码 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

但不幸的是仍然失败,因为 shopee.com.my 似乎有一个无效的 https 证书:

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

您应该使用官方文档推荐的 requests 模块。这也让事情变得更容易:

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)

输出:

<!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" />
    .....
    .....