标签:ascii码 port 转换 open 需要 htm encoding params param
import urllib.request
url = "http://www.baidu.com"
response = urllib.request.urlopen(url)
data = response.read()
# print(data)
# 将文件获取的内容转换成字符串
str_data = data.decode("utf-8")
print(str_data)
# 将结果保存到文件中
with open("baidu.html", "w", encoding="utf-8") as f:
f.write(str_data)
import urllib.request
def get_method_params(wd):
url = "http://www.baidu.com/s?wd="
# 拼接字符串
final_url = url + wd
# 发送网络请求
response = urllib.request.urlopen(final_url)
print(response.read().decode("utf-8"))
get_method_params("美女")
直接这么写会报错:
原因是,网址里面包含了汉字,但是ascii码是没有汉字的,需要转义一下:
import urllib.request
import urllib.parse
import string
def get_method_params(wd):
url = "http://www.baidu.com/s?wd="
# 拼接字符串
final_url = url + wd
# 将包含汉字的网址进行转义
encode_new_url = urllib.parse.quote(final_url, safe=string.printable)
# 发送网络请求
response = urllib.request.urlopen(encode_new_url)
print(response.read().decode("utf-8"))
get_method_params("美女")
标签:ascii码 port 转换 open 需要 htm encoding params param
原文地址:https://www.cnblogs.com/wbyixx/p/12231527.html