• 7、通过requests的session请求登录示例,并通过BeautifulSoup解析


    # Author:toloy
    import requests
    import json
    from bs4 import BeautifulSoup
    
    # 创建session对象
    sess = requests.session()
    # 登录的url
    url = "http://www.dfenqi.cn/User/Login"
    # 请求头
    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN",
        "Cache-Control": "no-cache",
        "Connection": "Keep-Alive",
        "Content-Type": "application/json; charset=utf-8",
        "Host": "www.dfenqi.cn",
        "Origin": "http://www.dfenqi.cn",
        "Referer": "http://www.dfenqi.cn/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299",
        "X-Requested-With": "XMLHttpRequest"
    }
    # 请求数据
    data = {
        "username":"13917475176",
        "password":"9688856b810f347416248a40236af572",
        "remember":"false",
        "AccountType":0,
        "ReturnUrl":None
    }
    # 返回响应文件
    response = sess.post(url,data = json.dumps(data),headers = headers)
    html = response.text
    # 转换成json对象
    result_json = json.loads(html)
    # 如果登录成功,则请求首页
    if result_json["Success"] == True:
        response = sess.get("http://www.dfenqi.cn/Product/Index",headers = headers)
        homePageHtml = response.text
        # 构建bs处理对象
        bs = BeautifulSoup(homePageHtml,"lxml")
        # bs可通过类似css选择器一样,按层级来选择
        menuList = bs.select(".zuonav .lanjiantou h3 a")
        for menu in menuList:
            print(menu.string)
    else:
        print("登录失败!")
    
  • 相关阅读:
    Test
    Python Requests库使用指南
    Python文件操作,看这篇就足够
    Ubuntu配置完全教程
    Redis入门
    Python Requests 库学习笔记
    c++11函数模板“偏特化”的一种实现
    c++通用判零模板类
    Python实现1-9数组形成的结果为100的所有运算式
    QT:用QWebSocket实现webchannel,实现C++与HTML通信
  • 原文地址:https://www.cnblogs.com/toloy/p/8621961.html
Copyright © 2020-2023  润新知