python爬虫之url中的中文问题
2021-06-29 20:06
标签:urlopen head 拼接 urlencode gecko use safari python enc 在python2中是 查看一下代码: 结果如下: 在python3中: 结果如下: 如果需要吧转换的字符变成中文 如下: python3 python爬虫之url中的中文问题 标签:urlopen head 拼接 urlencode gecko use safari python enc 原文地址:http://blog.51cto.com/853056088/2174896
我们想要访问的url就需要对url进行拼接,变成浏览器可以识别的url
在python中已经有了这样的模块了,这就是urlencode
urlencode需要对中文和关键字组成一对字典,然后解析成我们的url
urllib.urlencode(keyword)
在Python中是
urllib.parse.urlencode(keyword)
python2import 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(‘哈士奇‘))
# -*- 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
python2In [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=你好!
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=你好!‘
上一篇:计数排序 Rust实现
下一篇:数据结构及算法学习(一)