参考代码如下:
import requests
import json
url = "http://www.xinfadi.com.cn/getPriceData.html"
with open("data.csv",'w')as f:
f.writelines("品名,类型,最低价,平均价,最高价,产地,规格,发布日期\n")
for page in range(1,40):
FormData = {
'limit':20,
'current':page,
'prodPcatid': 1187,
'prodCatid': 1201
}
resp = requests.post(url,data=FormData)
html = resp.text
content = json.loads(html)
veg_info = content["list"]
print("品名\t类型\t最低价\t平均价\t最高价\t产地\t规格\t发布日期")
for dic in veg_info:
print(dic['prodName'],dic['prodCat'],dic['lowPrice'],dic['avgPrice'],dic['highPrice'],dic['place'],dic['unitInfo'],dic['pubDate'])
with open('data.csv','a',newline="\n") as f:
d = dic['prodName']+","+dic['prodCat']+","+dic['lowPrice']+","+dic['avgPrice']+","+dic['highPrice']+","+dic['place']+","+dic['unitInfo']+","+dic['pubDate']+"\n"
f.writelines(d)
运行如下:
|