python爬虫->北京新发地所有菜价 源代码

import requests
from concurrent.futures import ThreadPoolExecutor
import time
import csv


def get_data(page):
    url = 'http://www.xinfadi.com.cn/getPriceData.html'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
    }
    data = {
        'limit': 20,
        'current': page,
        'pubDateStartTime': '',
        'pubDateEndTime': '',
        'prodPcatid': '',
        'prodCatid': '',
        'prodName': ''
    }

    f = open('../../get_bug/csv/北京新发地all_vegetable_price', mode='a', newline='')
    csvwriter = csv.writer(f)
    resp = requests.post(url, headers=headers, data=data)
    lists = resp.json()['list']
    for item in lists:
        prodName = item['prodName']
        prodCat = item['prodCat']
        pubDate = item['pubDate']
        avgPrice = item['avgPrice']
        highPrice = item['highPrice']
        lowPrice = item['lowPrice']
        csvwriter.writerow([prodName, prodCat ,highPrice, lowPrice, avgPrice, pubDate])
        print('完成')
    resp.close()
    f.close()


if __name__ == '__main__':
    with ThreadPoolExecutor(80) as t:
        for i in range(1, 1 + 21651):
            t.submit(get_data, i)
            time.sleep(1)
        print('over!')
        t.shutdown()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值