# -*- coding:utf-8 -*-
#@Time : 2021/12/30 11:25
#@Author : dogge
#@File :test.py
#@software:PyCharm
import requests
import csv
class vegatable_data:
def __init__(self):
self.url = 'http://www.xinfadi.com.cn/getPriceData.html'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
self.formdata = {
'limit': '',
'current': '',
'pubDateStartTime': '',
'pubDateEndTime': '',
'prodPcatid': '',
'prodCatid': '',
'prodName': ''
}
# 使用office打开csv 需要使用utf-8-sig编码保存中文
# 使用wps 则序使用utf-8
file = open('蔬菜价格.csv','w',encoding='utf-8',newline='')
self.csv_file = csv.writer(file)
self.csv_file.writerow(['蔬菜名称','最低价格','最高价格','平均价格','产地','更新时间'])
def parse(self,data):
list = data['list']
for cur in list:
prodName = cur['prodName']
lowPrice = cur['lowPrice']
highPrice = cur['highPrice']
avgPrice = cur['avgPrice']
place = cur['place']
pubDate = cur['pubDate']
self.csv_file.writerow([prodName,lowPrice,highPrice,avgPrice,place,pubDate])
def get_data(self):
for i in range(1,11):
print("第{}页".format(i))
self.formdata['current']=i;
resp = requests.post(url=self.url,data=self.formdata,headers=self.headers)
data = resp.json()
self.parse(data)
if __name__ == '__main__':
v = vegatable_data()
v.get_data()
爬取北京新发地蔬菜价格
最新推荐文章于 2024-08-30 00:58:42 发布