数据写入csv文件时出错
import requestsimport csv
from concurrent.futures importThreadPoolExecutor
def get_url(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/103.0.0.0 Safari/537.36',
'Referer' : 'http://www.xinfadi.com.cn/priceDetail.html'
}
data = {
'limit': 20,
'current': page,
'pubDateStartTime': '',
'pubDateEndTime': '',
'prodPcatid': '',
'prodCatid': '',
'prodName': ''
}
Name = []
LowPrice= []
AvgPrice = []
High_price = []
SpecInfo= []
Place = []
UnitInfo= []
Date = []
response = requests.post(url,headers=headers,data=data)
for i in range(20):
list1 = []
name = response.json()['list']['prodName']
lowPrice = response.json()['list']['lowPrice']
avgPrice = response.json()['list']['avgPrice']
high_price = response.json()['list']['highPrice']
specInfo = response.json()['list']['specInfo']
place = response.json()['list']['place']
unitInfo = response.json()['list']['unitInfo']
date = response.json()['list']['pubDate']
# Name.append(name)
# LowPrice.append(lowPrice)
# AvgPrice.append(avgPrice)
# High_price.append(high_price)
# SpecInfo.append(specInfo)
# Place.append(place)
# UnitInfo.append(unitInfo)
# Date.append(date)
list1.extend((name,lowPrice,avgPrice,high_price,specInfo,place,unitInfo,date))
print(list1)#打印所需要的数据
save_Data(list1)#写入所需的数据,此处写入的数据不对
print('第%d页爬取完毕'% page)
def save_Data(data): #目标函数
with open('price.csv','w',encoding='utf-8' ) as f:
csvwriter = csv.writer(f)
csvwriter.writerow(data)
if __name__ == '__main__':
for i in range(1,10):
datas = get_url(i)
首先要保证你爬到的数据是对的 wp231957 发表于 2022-7-27 14:21
首先要保证你爬到的数据是对的
首先要确定你看了代码 以w模式写入就会那样造成数据覆盖,以a模式追加写入就好 ZhKQYu 发表于 2022-7-27 16:42
以w模式写入就会那样造成数据覆盖,以a模式追加写入就好
def save_Data(data): #目标函数
with open('price.csv','a',encoding='ANSI' ) as f:
csvwriter = csv.writer(f)
csvwriter.writerow(data)
页:
[1]