zoukankan      html  css  js  c++  java
  • 对于服务器的识别的条件,header之类的使用

    根据上一节的内容的衔接

    一:urllib.request的使用

    headers的一些属性

    User-Agent : 有些服务器或 Proxy 会通过该值来判断是否是浏览器发出的请求
    Content-Type : 在使用 REST 接口时,服务器会检查该值,用来确定 HTTP Body 中的内容该怎样解析。
    application/xml : 在 XML RPC,如 RESTful/SOAP 调用时使用
    application/json : 在 JSON RPC 调用时使用
    application/x-www-form-urlencoded : 浏览器提交 Web 表单时使用
    在使用服务器提供的 RESTful 或 SOAP 服务时, Content-Type 设置错误会导致服务器拒绝服务

    根据上面的情况,我们可以知道,user-Agent是身份,有些服务器会根据身份来是否得到响应,所以则需要设置User-Agent u

    对于获得header,可以使用fiddler来获取

    如图所示:

    url = 'http://www.server.com/login'

    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  
    values = {'username' : 'cqc',  'password' : 'XXXX' }  
    headers = { 'User-Agent' : user_agent }  
    data =urllib.parse.urlencode(values)  
    request =urllib.request.Request(url, data, headers)  
    response = urllib.request.urlopen(request)  
    page = response.read()
    二:requests的高级使用
    设置header是一样的作用
    例子:
    headers={'user-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}
    html=requests.get('http://oa.gzcc.cn/index.php?m=content&c=index&a=show&catid=42&id=967',headers=headers)
    效果十一样的
    因为selenium是本来就是使用服务器,所以这个不需要设置
  • 相关阅读:
    SpringBoot入门
    VUE 监听局部滚动 设置ICON的位置跟随
    手机端页面调试工具-vconsole使用
    js获取字符串字节的位数
    判断数据为[] {} /空数组或空对象
    Vue axios 上传图片
    Vue触发input选取文件点击事件
    腾讯地图添加多个Marker
    VUE-CLI 设置页面title
    小程序wxml文件引用方式
  • 原文地址:https://www.cnblogs.com/caicaihong/p/5687441.html
Copyright © 2011-2022 走看看