python爬虫之post请求

import requests
import json
import openpyxl

url1 = 'https://a300010770.casmart.com.cn/shop/products'
headers = {
     'content-type': 'application/json;charset=UTF-8',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '}

# payload = {'initPage': 'false',
# 'pageNum': 2,
# 'pageSize': 20,
# 'orderBy': "",
# 'supplierCateId': "-1",
# 'queryString': "",
# 'minPrice': 'null',
# 'maxPrice': 'null',
# 'queryType': "pro"
# }

# 创建excel
xls1 = openpyxl.Workbook()
# 激活sheet
sheet = xls1.active
# 要保存的数据列名
title =['brandAgent','brandName','proCode']
# 添加列名
sheet.append(title)

for i in range(1,10):
    payload = {
     'pageNum':i}
    rsp = requests.post(url=url1,headers=headers,data=json.dumps(payload))
    json_dic= json.loads(rsp.text)
    print('正在抓取第',i,"页")

    # print(type(json_dic))
    # print(json_dic)

    for i in range(20):
        brandAgent = json_dic['dataList'][i]['brandAgent']
        brandName = json_dic['dataList'][i]['brandName']
        proCode = json_dic['dataList'][i]['proCode']
        sheet.append([brandAgent,brandName,proCode])

xls1.save('zizi.xlsx')





你可能感兴趣的:(python爬虫)