defSavepm25(webs,path): for web in webs: print('正在下载数据...'+web) data = rq.get(web,headers=headers).text soup = BeautifulSoup(data, 'lxml') pms = soup.find_all('td') aqis = [] for pm in pms: data = pm.get_text() aqis.append(data) i = 0 while i < 12: aqis.pop() i = i + 1 withopen(path+'.csv', 'a+') as f: i = 1 for aqi in aqis: if i % 11 != 0: f.write(aqi + ',') # CSV表格里以','为分隔符 else: f.write(aqi + '\r\n') #若是行尾则换行 i = i + 1
这里with open(path+'.csv', 'a+') as f:,'a+'是以追加的方式添加数据到CSV文件。
保存为 CSV
最后写一个循环来分别以 城市名.CSV 来保存到本地。
1 2 3 4 5 6 7 8 9 10 11 12
paths = [] for cityname in citynames: path = cityname + 'pm25' paths.append(path) i = 0 for url in cityurls: url = Url+url print(url) webs=getwebs(url) path = paths[i] Savepm25(webs,path) i = i + 1