2021-02-21

Python 的urllib模块模拟使用

import urllib
response  = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode("utf-8"))
  # 获取网页源码进行utf-8解析可以防止乱码




# 获取一个post请求
import urllib.parse,urllib.request# # data = bytes(urllib.parse.urlencode({"holle":"world"}),encoding="utf-8")



  # 转换成二进制
response = urllib.request.urlopen("http://httpbin.org/pos",data = data)
print(response.read().decode("utf-8"))





# 超时处理
try:
     response = urllib.request.urlopen("http://httpbin.org/get",timeout=1)
     print(response.read().decode("utf-8"))
      except urllib.error.URLError as e:
           print("time out")





# 伪装
import urllib.requesturl =
"http://.douban.com"
headers = {    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68"}
req = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(req)print(response.read().decode("utf-8"))
上一篇:老卫带你学---安装python包出现PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。


下一篇:java中的URL编码与解码方法