|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
import csv
from concurrent.futures import ThreadPoolExecutor
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'][i]['prodName']
lowPrice = response.json()['list'][i]['lowPrice']
avgPrice = response.json()['list'][i]['avgPrice']
high_price = response.json()['list'][i]['highPrice']
specInfo = response.json()['list'][i]['specInfo']
place = response.json()['list'][i]['place']
unitInfo = response.json()['list'][i]['unitInfo']
date = response.json()['list'][i]['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)
def save_Data(data): #目标函数
with open('price.csv','a',encoding='ANSI' ) as f:
csvwriter = csv.writer(f)
csvwriter.writerow(data)
|
|