1. # 不带参数,get请求
    2. import urllib.request
    3. def load_data():
    4. url = "http://www.baidu.com/"
    5. #get的请求
    6. #http请求
    7. #response:http相应的对象
    8. response = urllib.request.urlopen(url)
    9. print(response)
    10. #读取内容 bytes类型
    11. data = response.read()
    12. print(data)
    13. #将文件获取的内容转换成字符串
    14. str_data = data.decode("utf-8")
    15. print(str_data)
    16. #将数据写入文件
    17. with open("baidu.html","w",encoding="utf-8")as f:
    18. f.write(data)
    19. #将字符串类型转换成bytes
    20. str_name = "baidu"
    21. bytes_name =str_name.encode("utf-8")
    22. print(bytes_name)
    23. #python爬取的类型:str bytes
    24. #如果爬取回来的是bytes类型:但是你写入的时候需要字符串 decode("utf-8")
    25. #如果爬取过来的是str类型:但你要写入的是bytes类型 encode(""utf-8")
    26. load_data()
    27. -------------------------------------------------------------------------------------------------------------
    28. #带参数
    29. import urllib.request
    30. import urllib.parse
    31. import string
    32. def get_method_params():
    33. url = "http://www.baidu.com/s?wd="
    34. #拼接字符串(汉字)
    35. #python可以接受的数据
    36. #https://www.baidu.com/s?wd=%E7%BE%8E%E5%A5%B3
    37. name = "美女"
    38. final_url = url+name
    39. print(final_url)
    40. #代码发送了请求
    41. #网址里面包含了汉字;ascii是没有汉字的;url转译
    42. #将包含汉字的网址进行转译
    43. encode_new_url = urllib.parse.quote(final_url,safe=string.printable)
    44. print(encode_new_url)
    45. # 使用代码发送网络请求
    46. response = urllib.request.urlopen(encode_new_url)
    47. print(response)
    48. #读取内容
    49. data = response.read().decode()
    50. print(data)
    51. #保存到本地
    52. with open("02-encode.html","w",encoding="utf-8")as f:
    53. f.write(data)
    54. #UnicodeEncodeError: 'ascii' codec can't encode
    55. # characters in position 10-11: ordinal not in range(128)
    56. #python:是解释性语言;解析器只支持 ascii 0 - 127
    57. #不支持中文
    58. get_method_params()