url中含有中文的处理
Python编程:URL网址链接中的中文编码与解码
Python进行URL解码
from urllib.request import quote
...
def start_requests(self):keywords = ['手机', '笔记本电脑', '键鼠套装']
for keyword in keywords:
url = r'https://s.taobao.com/search?q={}'.format(keyword)
ret1 = quote(url, safe=";/?:@&=+$,", encoding="utf-8")
yield scrapy.Request(url=ret1)
# -*- coding: utf-8 -*-
# @File : urldecode_demo.py
# @Date : 2018-05-11
from urllib.request import quote, unquote
# 编码
url1 = "https://www.baidu.com/s?wd=中国"
# utf8编码,指定安全字符
ret1 = quote(url1, safe=";/?:@&=+$,", encoding="utf-8")
print(ret1)
# https://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD
# gbk编码
ret2 = quote(url1, encoding="gbk")
print(ret2)
# https%3A//www.baidu.com/s%3Fwd%3D%D6%D0%B9%FA
# 解码
url3 = "https://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD"
ret3 = unquote(url3, encoding='utf-8')
print(ret3)
# https://www.baidu.com/s?wd=中国
python字符串前加r、f、u、l 的区别
python中 r'', b'', u'', f'' 的含义
含有中文的url前加u 亦可!
def start_requests(self):keywords = ['手机', '笔记本电脑', '键鼠套装']
for keyword in keywords:
url = u'https://s.taobao.com/search?q={}'.format(keyword)
yield scrapy.Request(url=url)
标签:baidu,www,Python,quote,笔记,语法,url,https,com
From: https://www.cnblogs.com/zhlforhe/p/18014701