import requests
url ="http://www.xinfadi.com.cn/getPriceData.html"
f = open("蔬菜价格.csv", mode="w", encoding="utf-8")
date = {
    "limit":"",
    "current":"",
    "pubDateStartTime":"" ,
    "pubDateEndTime":"",
    "prodPcatid":"" ,
    "prodCatid":"" ,
    "prodName":""
}
resp = requests.post(url,data=date) # post 请求需要添加参数,返回json()对象【就是字典】
page = resp.json()["list"] # 返回的时json数据 用{ }括起来; 其实就是字典  【区别】resp.text()拿到的时字符串【文本】
times = len(page) # 取出字典的长度
for i in range(times): # 根据字典长度进行循环取字典里面的值
    a = page[i] # 拿到第一列所有的字典
    prodName = a["prodName"]
    lowPrice = a["lowPrice"]
    avgPrice = a["avgPrice"]
    highPrice = a["highPrice"]
    place = a["place"]
    unitInfo = a["unitInfo"]
    pubDate = a["pubDate"]
    print(prodName,lowPrice,avgPrice,highPrice,place,unitInfo,pubDate)
    f.write(f"{prodName},{lowPrice},{avgPrice},{highPrice},{place},{unitInfo},{pubDate}\n") # 将数据写到文件,记住不要忘记换行符
print("恭喜,数据提取完毕!")