01-新发地 数据爬取 post 请求[原网页找不到内容]

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("恭喜,数据提取完毕!")







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值