zoukankan      html  css  js  c++  java
  • spider_使用cookie模拟登录

    """
    使用cook模拟登陆(反 登录)
    """
    from urllib import request
    import chardet

    def baiDu():
    url = "https://www.baidu.com/"
    headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
    "Cookie":"BAIDUID=954E1AC8A830CE77E8B1E19CD1659681:FG=1; BIDUPSID=954E1AC8A830CE77E8B1E19CD1659681; PSTM=1555899179; BD_UPN=12314753; BDUSS=0ZzMFpSbzVEeTZDSnRIamRMazM3UnM3QTVxWHNvS1RubllveGZaZkI2Y1Z2UmhkSVFBQUFBJCQAAAAAAAAAAAEAAAC~TsVzuf6wobCh0b25~tG9AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABUw8VwVMPFcc; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; H_PS_PSSID=26522_1431_21098_29073_20880_28518_29099_29134_28831_28584_22160; sugstore=1"
    }
    # 1.使用ip代理,构建字典
    proxy = {
    "http":"182.34.32.54:39219"
    }
    # 2.构建代理处理器
    proxyHandler= request.ProxyHandler(proxy)
    # 3.构建一个url打开器
    opener = request.build_opener(proxyHandler)
    # 4.安装url打开器
    request.install_opener(opener)
    # 将user-agent注入到request请求里
    req=request.Request(url, headers=headers)
    # 获取字节流网页源代码
    bytesHtml =request.urlopen(req).read()
    # 解析网页编码格式,返回一个字典
    htmlEncode=chardet.detect(bytesHtml)
    # 二进制代码解码
    html = bytesHtml.decode(htmlEncode["encoding"])
    return html

    bd = baiDu()
    print(bd)
    人生苦短,我用python!
  • 相关阅读:
    Fody is only supported on MSBuild 16 and above
    abp发送邮件AbpMailKit
    看一位老司机的博文,分享一下。
    nginx PC 移动配置
    微信开放平台登录
    flask 中 session的源码解析
    python mac环境搭建
    前端换mac可以参考搭一下简单的环境
    vue 导航钩子
    HTML5 History 模式
  • 原文地址:https://www.cnblogs.com/YangQingHong/p/10971574.html
Copyright © 2011-2022 走看看