繁体   English   中英

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

我正在尝试阅读此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)>

您应该使用官方文档推荐的请求模块。 它也更容易:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM