新发地新_异步_json_偶尔一俩个有问题

import asyncio
import aiofiles
import aiohttp


async def xin(url, data):
headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'http://www.xinfadi.com.cn',
'Referer': 'http://www.xinfadi.com.cn/priceDetail.html',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188',
'X-Requested-With': 'XMLHttpRequest',
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=data) as resp:
dic = await resp.json()
for item in dic['list']:
# print(item)
name = item["prodName"]
low = item["lowPrice"]
jun = item["avgPrice"]
high = item["highPrice"]
spec = item["specInfo"]
place = item["place"]
comp = item["unitInfo"]
date = item["pubDate"]
# print(name, low, jun, high, spec, place, comp, date)

async with aiofiles.open("文件/菜价.csv", mode="a", encoding="utf-8") as f:
await f.write(','.join([name, low, jun, high, spec, place, comp, date]))
await f.write("\n")


async def get_all(url):
tasks = []
for i in range(1, 5):
data = {
'limit': '20',
'current': f'{i}',
'pubDateStartTime': '',
'pubDateEndTime': '',
'prodPcatid': '',
'prodCatid': '',
'prodName': '',
}
t = asyncio.create_task(xin(url, data))
tasks.append(t)
await asyncio.wait(tasks)


def main():
url = "http://www.xinfadi.com.cn/getPriceData.html"
asyncio.run(get_all(url))
# tasks.append(asyncio.create_task(xin(url)))
# await asyncio.wait(tasks)


if __name__ == '__main__':
main()
print("over!!")
posted @ 2023-08-03 22:33  严永富  阅读(2)  评论(0)    收藏  举报