在python的爬虫学习中,我们的url经常出现中文的问题,
我们想要访问的url就需要对url进行拼接,变成浏览器可以识别的url
在python中已经有了这样的模块了,这就是urlencode
urlencode需要对中文和关键字组成一对字典,然后解析成我们的url
在python2中是
urllib.urlencode(keyword)
在Python中是
urllib.parse.urlencode(keyword)
查看一下代码:
python2
import urllib
import urllib2
#例如我们需要在百度上输入个关键字哈士奇进行查询,但是哈士奇是中文的,我们需要对哈士奇进行编码
keyword = {"wd":"哈士奇"}
head_url = "http://www.baidu.com/s"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
wd = urllib.urlencode(keyword)
url = head_url +"?"+ wd
req = urllib2.Request(url,headers=headers)
response = urllib2.urlopen(req)
html = response.read()
print(url)
print(html.count(‘哈士奇‘))
结果如下:
在python3中:
# -*- coding: utf-8 -*-
# File : url中出现的中文问题.py
# Author: HuXianyong
# Date : 2018-09-13 17:39
from urllib import request
import urllib
#例如我们需要在百度上输入个关键字哈士奇进行查询,但是哈士奇是中文的,我们需要对哈士奇进行编码
keyword = {"wd":"哈士奇"}
head_url = "http://www.baidu.com/s"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
wd = urllib.parse.urlencode(keyword)
url = head_url +"?"+ wd
req = request.Request(url,headers=headers)
response = request.urlopen(req)
html = response.read()
print(html.decode().count("哈士奇"))
print(url)
结果如下:
如果需要吧转换的字符变成中文
可以用unquota
如下:
python2
In [25]: dic = {"say":"你好!"}
In [26]: urllib.urlencode(dic)
Out[26]: ‘say=%E4%BD%A0%E5%A5%BD%21‘
In [27]: aa = urllib.urlencode(dic)
In [28]: aa
Out[28]: ‘say=%E4%BD%A0%E5%A5%BD%21‘
In [29]: bb = urllib.unquote(aa)
In [30]: bb
Out[30]: ‘say=\xe4\xbd\xa0\xe5\xa5\xbd!‘
In [31]: print(bb)
say=你好!
python3
In [16]: dic = {"say":"你好!"}
In [17]: aa = urllib.parse.urlencode(dic)
In [18]: aa
Out[18]: ‘say=%E4%BD%A0%E5%A5%BD%21‘
In [19]: bb = urllib.parse.unquote(aa)
In [20]: bb
Out[20]: ‘say=你好!‘
原文地址:http://blog.51cto.com/853056088/2174896
时间: 2024-12-11 17:40:30