"""
@author: wangxingchun
多線程(線程池)
下載數(shù)據(jù)
"""
import requests
import csv
from concurrent.futures import ThreadPoolExecutor as tp
#創(chuàng)建一個(gè)csv文件,注意創(chuàng)建writer對(duì)象"csv.writer()"
f = open('xinfadi.csv','w',encoding='utf8')
csvwrite = csv.writer(f)
#如果寫入txt文件,不需要?jiǎng)?chuàng)建writer對(duì)象。
# f = open('xinfadidata.txt','w',encoding='utf8')
#創(chuàng)建一個(gè)函數(shù),以頁碼做為參數(shù)
def down(n_page):
url = 'http://www.xinfadi.com.cn/getPriceData.html'
data = {'count': 428225,'current': n_page,'limit': 20}
resp = requests.post(url,data=data)
datas =resp.json()
#通過分析數(shù)據(jù)嵌套情況,獲取數(shù)據(jù)。此處可在網(wǎng)頁開發(fā)工具json數(shù)據(jù)中查看分析。
for i in range(len(datas['list'])):
name = datas['list'][i]['prodName']
highPrice = datas['list'][i]['highPrice']
lowPrice = datas['list'][i]['lowPrice']
pubDate = datas['list'][i]['pubDate']
place = datas['list'][i]['place']
csvwrite.writerow((name,highPrice,lowPrice,pubDate,place))#writerow要求寫入的是可迭代對(duì)象
# f.writelines(f'{name},{highPrice},{lowPrice},{pubDate},{place}n')
resp.close()
if __name__ == '__main__':
with tp(50) as t: #創(chuàng)建線程池,
for n in range(1,101): #遍歷數(shù)據(jù)網(wǎng)頁
t.submit(down,n) #提交給線程池,進(jìn)行多線程下載
print(f'共{n}頁數(shù)據(jù)下載完畢!')
f.close()