爬取北京新發地蔬菜價格

有一只巨肥的zhu 2022-01-07 07:40:19 阅读数:673

北京 蔬菜

 北京新發地

# -*- 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()

版权声明:本文为[有一只巨肥的zhu]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201070740194602.html