Python爬虫:urllib库的基本使用
Python爬虫:urllib库的基本使用 Python 爬虫 请求网址获取网页代码 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) get带参数请求 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( "美女" ) 直接这么写会报错: 原因是,网址里面包含了汉字