requests post请求,加上会话功能 以及url 编码问题

import requests
from urllib.parse import urlencode
from openpyxl import Workbook

requests = requests.session()

login_url = "https://passport.simuwang.com/index.php?m=passport&c=auth&a=login"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36",
    "Referer": "https://dc.simuwang.com/"
}

data = {
    "username": "13074703598",
    "password": "xxxxx",
    "do_qualified": "1",
    "reme": "1"
}
res = requests.post(url=login_url, headers=headers, data=data)


def detail(page):
    params = {
        "page": "%s" % page,
        "condition": "fund_type:1,6,4,3,8,2;rating_year:1;istiered:0;company_type:1;city:北京;sort_name:profit_col1;sort_asc:desc;keyword:",
        "type": "0",
        "selected": "0"
    }
    url = "https://dc.simuwang.com/"
    url = url + 'ranking/get?%s' % urlencode(params)
    res1 = requests.get(url, headers=headers)
    dict1 = res1.json()

    dict_list = dict1.get('data')
    dict_strategy = {
        '1': '股票策略',
        '8': '复合策略',
        '7': '组合策略',
        '6': '固定收益',
        '2': '宏观策略',
        '3': '管理期货',
        '4': '事件驱动',
        '5': '相对价值'
    }
    # print(dict_list)
    for dict_detail in dict_list:
        fund_name = dict_detail.get('fund_name')  # 基金简称
        strategy1 = dict_detail.get('strategy')  # 投资策略
        strategy = dict_strategy.get(strategy1)
        company_short_name = dict_detail.get('company_short_name')  # 基金公司
        profit_col1 = dict_detail.get('profit_col1')  # 最新净值

        price_date = dict_detail.get('price_date')  # 日期

        profit_col11 = dict_detail.get('profit_col11')  # 年化

        profit_col2 = dict_detail.get('profit_col2')  # 近一月

        profit_col3 = dict_detail.get('profit_col3')  # 近三月

        profit_col4 = dict_detail.get('profit_col4')  # 近半年

        profit_col5 = dict_detail.get('profit_col5')  # 近一年

        profit_col6 = dict_detail.get('profit_col6')  # 近两年
        wb1.append(
            [fund_name, strategy, company_short_name, profit_col1, price_date, profit_col11, profit_col2, profit_col3,
             profit_col4, profit_col5, profit_col6])
        print(fund_name, strategy, company_short_name, profit_col1, price_date, profit_col11, profit_col2, profit_col3,
              profit_col4, profit_col5, profit_col6)


wb = Workbook()  # 先生成一个工作簿
wb1 = wb.create_sheet('index', 0)
wb1.append(['基金简称', '投资策略', '基金公司', '最新净值', '日期', '年化', '近一月', '近三月', '近半年', '近一年', '近两年'])
for i in range(1, 158):
    detail(i)
    print(i)

wb.save('cik.xlsx')

 路径编码

>>> from urllib.parse import quote
>>> quote('/test')
'/test'
>>> quote('/test', safe='')
'%2Ftest'
>>> quote('/El Niño/')
'/El%20Ni%C3%B1o/'

 携带cookie

import requests
session = requests.session()

cookie = "addf=54654654654; fgsjsgf=feurgrehgtrhbrtjbrtjh;"
def cookie_to_cookiejar(cookies):
    if not hasattr(cookies, "startswith"):
        raise TypeError
    import requests
    cookiejar = requests.utils.cookiejar_from_dict(
        {cookie[0]: cookie[1] for cookie in
         [cookie.split("=", maxsplit=1) for cookie in cookies.split(";")]})
    return cookiejar


cookiejar = cookie_to_cookiejar(cookie.strip('; '))
session.cookies = cookiejar
原文地址:https://www.cnblogs.com/wukai66/p/12882751.html