Python 中的爬虫 URL 包含中文如何转义?

作者: 王炳明 分类: Python 基础教程 发布时间: 2022-11-06 16:56 热度:291

python3 解法

from urllib.request import quote, unquote
url = "https://www.baidu.com/s?wd=code123中国"
gbk_url = quote(url, safe=";/?:@&=+,", encoding="gbk")
utf_url = quote(url, safe=";/?:@&=+,", encoding="utf-8")
print("gbk_url: %s" % gbk_url)
print("utf_url: %s" % utf_url)

unquote_gbk_url = unquote(gbk_url, encoding="gbk")
unquote_utf_url = unquote(utf_url, encoding="utf-8")

print("un_gbk_url: %s" % unquote_gbk_url)
print("un_utf_url: %s" % unquote_utf_url)

python2 解法

# -*- coding: utf-8 -*- 

from urllib import quote, unquote
import sys 

url = "https://www.baidu.com/s?wd=code123中国" 
gbk_url = quote(url.decode(sys.stdin.encoding).encode('gbk'), safe=";/?:@&=+,")
utf_url = quote(url.decode(sys.stdin.encoding).encode('utf-8'), safe=";/?:@&=+,")
print("gbk_url: %s" % gbk_url)
print("utf_url: %s" % utf_url)

文章有帮助,请作者喝杯咖啡?

发表评论