python urllib模块

urllib.request.urlopen

urllib.request.urlopen = urlopen(url, data=None, timeout=<object objectat 0x7f66fb3ab140>, *, cafile=None, capath=None, cadefault=False, context=None)

发送get请求

import urllib.request
response=urllib.request.urlopen("https://www.baidu.com")
print(response.read().decode('utf-8'))
方法简介演示
open()获取网页内容 response.open()
status获取网站状态码(200、301、404等)response.status
getheaders()获取网站响应头部response.getheaders()
getheader('name')获取相应头部获取传入name对应值
response.getheader('Server')

使用getheaders()获取响应头部,使用getheader('Server')获取Web服务器使用的是BWS/1.1 

 

发送post数据

urlopen除了url参数还有许多的参数,使用data就是使用post请求。发送post需要使用字节流,使用了batas,第一个参数是字符串,第二个参数是编码格式。 

import urllib.request
import urllib.parse
postdata=bates(urllib.parse.urlencode({"word":"123456"}),"utf8")
response=urllib.request.urlopen("http://192.168.1.90",data=postdata) 
print(response.read().decode('utf-8'))


urllib.request.Request

class urllib. request. Request ( url, data=None, headers={}, origin_req_host=None, unverifiable=False, method =None)
  • url,请求url,必传参数
  • data,要传需要使用字节流类型
  • header,请求头,是个字典,可以使用 add_header() 添加
  • origin_req_host,请求方host或ip
  • method,请求方式,GET、POST、PUT等默认POST
from urllib import request
from urllib import parse
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}
data=tybes(parse.urlencode({'snow','123456'}),encode='utf8')



(这个模块就当我没用过吧,不写了,不学了)

none

发表评论

0条评论

设置
配色方案

布局

现在购买