python新发地每日菜价提取

import requests
import csv
import time


class price_spider(object):
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
            "Referer": "http://www.xinfadi.com.cn/priceDetail.html",
            "Cookie": "SHOP_MANAGE=c5dbec72-aa12-49b1-8557-ba67bad6bbb6"}
        self.prodCat=[]
        self.prodPcat=[]
        self.prodName = []
        self.lowPrice = []
        self.avgPrice = []
        self.highPrice = []
        self.specInfo = []
        self.place = []
        self.unitInfo = []
        self.pubDate = []
        self.session = requests.session()

    def get_response(self, url, data):
        response = self.session.post(url, data=data, headers=self.headers)
        dic = response.json()
        return dic

    def parse_data(self, dic):
        price_list = dic['list']
        for test in range(1, len(price_list)):
            info = price_list[test]
            print(info)
            self.prodCat.append(info.get('prodCat'))
            self.prodPcat.append(info.get('prodPcat'))
            self.prodName.append(info.get('prodName'))
            self.lowPrice.append(info.get('lowPrice'))
            self.avgPrice.append(info.get('avgPrice'))
            self.highPrice.append(info.get('highPrice'))
            self.specInfo.append(info.get('specInfo'))
            self.place.append(info.get('place'))
            self.unitInfo.append(info.get('unitInfo'))
            self.pubDate.append(info.get('pubDate'))

    def save_data(self):
        rows = zip(self.prodCat,self.prodPcat,self.prodName, self.lowPrice, self.avgPrice, self.highPrice, self.specInfo, self.place,
                   self.unitInfo, self.pubDate)
        with open('price.csv', 'w', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)
            writer.writerow(["一级分类","二级分类","品名", "最低价", "平均价", "最高价", "规格", "产地", "单位", "发布日期"])
            for row in rows:
                writer.writerow(row)

    def run(self):
        start = time.clock()
        base_url = 'http://www.xinfadi.com.cn/getPriceData.html'
        for i in range(1, 25):
            d = {"limit": 20,"current": f"{i}","pubDateStartTime": "2023/07/03","pubDateEndTime": "2023/07/03"}
            dic = self.get_response(base_url, data=d)
            self.parse_data(dic)
        self.save_data()
        end = time.clock()
        print('Running time: %s Seconds' % (end - start))

price_spider().run()

class price_spider(object):    def __init__(self):        self.headers = {            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",            "Referer": "http://www.xinfadi.com.cn/priceDetail.html",            "Cookie": "SHOP_MANAGE=c5dbec72-aa12-49b1-8557-ba67bad6bbb6"}        self.prodCat=[]        self.prodPcat=[]        self.prodName = []        self.lowPrice = []        self.avgPrice = []        self.highPrice = []        self.specInfo = []        self.place = []        self.unitInfo = []        self.pubDate = []        self.session = requests.session()
    def get_response(self, url, data):        response = self.session.post(url, data=data, headers=self.headers)        dic = response.json()        return dic
    def parse_data(self, dic):        price_list = dic['list']        for test in range(1, len(price_list)):            info = price_list[test]            print(info)            self.prodCat.append(info.get('prodCat'))            self.prodPcat.append(info.get('prodPcat'))            self.prodName.append(info.get('prodName'))            self.lowPrice.append(info.get('lowPrice'))            self.avgPrice.append(info.get('avgPrice'))            self.highPrice.append(info.get('highPrice'))            self.specInfo.append(info.get('specInfo'))            self.place.append(info.get('place'))            self.unitInfo.append(info.get('unitInfo'))            self.pubDate.append(info.get('pubDate'))
    def save_data(self):        rows = zip(self.prodCat,self.prodPcat,self.prodName, self.lowPrice, self.avgPrice, self.highPrice, self.specInfo, self.place,                   self.unitInfo, self.pubDate)        with open('price.csv', 'w', newline='', encoding='utf-8') as f:            writer = csv.writer(f)            writer.writerow(["一级分类","二级分类","品名", "最低价", "平均价", "最高价", "规格", "产地", "单位", "发布日期"])            for row in rows:                writer.writerow(row)
    def run(self):        start = time.clock()        base_url = 'http://www.xinfadi.com.cn/getPriceData.html'        for i in range(1, 25):            d = {"limit": 20,"current": f"{i}","pubDateStartTime": "2023/07/03","pubDateEndTime": "2023/07/03"}            dic = self.get_response(base_url, data=d)            self.parse_data(dic)        self.save_data()        end = time.clock()        print('Running time: %s Seconds' % (end - start))
price_spider().run()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发地是中国北京市的蔬菜批发市场,每天都有大量的蔬菜价格行情更。想要用Python爬取发地的价格行情,可以通过以下几个步骤来实现。 首先,需要导入相关的Python库,包括requests和beautifulsoup库。requests库用于发送HTTP请求获取网页的内容,beautifulsoup库用于解析网页的内容。 接下来,需要分析发地价格行情所在的网页结构。可以通过打开发地的价格行情网页,查看网页的源代码来分析。一般来说,每个蔬菜的价格信息都会包含在一个HTML标签中,可以通过查找这个标签来筛选出所需的信息。 然后,使用Python编写代码来发送HTTP请求并获取价格行情网页的内容。可以使用requests库的get方法来发送GET请求,并使用其返回的response对象的text属性来获取网页的内容。 接着,使用beautifulsoup库来解析网页的内容。可以使用beautifulsoup库的BeautifulSoup类来初始化一个解析器对象,并使用其find_all方法来查找指定的HTML标签。通过查找所需标签,可以获取到蔬菜的名称和对应的价格信息。 最后,将获取到的价格信息进行整理和格式化,并保存到文件中或者打印出来。 需要注意的是,爬取网页的过程中需要注意遵守相关的法律法规,并尊重网站的使用规则,不要对网站进行过多的频繁请求,以免给网站带来压力。 通过以上步骤,就可以用Python爬取发地的价格行情了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值