# get请求练习
import urllib.request
import urllib.parse
import string
def get_params():
url = "http://www.baidu.com/s?"
params = {
"wd": "中文",
"key": "zhang",
"value": "san"
}
# 要将字典中的":"变成字符串中的“=”
str_parse = urllib.parse.urlencode(params)
final_url = url + str_parse
print(final_url)
# https://www.baidu.com/s?wd=%E4%B8%AD%E6%96%87&key=zhang&value=san
# 将带有中文的url转译成计算机可识别的url
end_url = urllib.parse.quote(final_url, safe=string.printable)
response = urllib.request.urlopen(end_url)
# 读取返回的二进制文件
content = response.read()
# 将二进制文件行进源码解析
origion = content.decode("utf-8")
print(origion)
# 将此文件保存在xml内
with open("张三.html", "w", encoding="utf-8") as f:
f.write(origion)
get_params()`在这里插入代码片`
【爬虫基础】day01 get请求练习
猜你喜欢
转载自blog.csdn.net/xiaoyaosheng19/article/details/89207973
今日推荐
周排行