20180213 爬蟲爬取空氣質量資料
阿新 • • 發佈:2018-11-09
目標網址:
空氣質量歷史資料
1、修改爬蟲原因:網址針對爬蟲作了防範措施,直接爬取很難奏效。
2、google 的webdriver難以get內容,也許是網站針對性的進行了防範
思路:
1、利用Cenenium+PlatformJS 模擬瀏覽器請求一個頁面
2、Pandas裡面的read_html函式讀取頁面中的表格資料
環境:
python3.6+Cenenium+PlatformJS,安裝過程略
程式碼:
# coding:utf-8
import time
from urllib import parse
import pandas as pd
from selenium import webdriver
driver = webdriver.PhantomJS('D:\\webdriver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
base_url = 'https://www.aqistudy.cn/historydata/daydata.php?city='
def get_month_set():
month_set = list()
for i in range(1, 10):
month_set.append(('2014-0%s' % i))
for i in range(10, 13):
month_set.append(('2014-%s' % i))
for i in range(1, 10):
month_set.append(('2015-0%s' % i))
for i in range(10, 13):
month_set.append(('2015-%s' % i))
for i in range(1, 10):
month_set.append(('2016-0%s' % i))
for i in range(10, 13):
month_set.append(('2016-%s' % i))
for i in range(1, 10):
month_set.append(('2017-0%s' % i))
for i in range(10, 13):
month_set.append(('2017-%s' % i))
return month_set
def get_city_set():
str_file = r'city.txt'
fp = open(str_file,'rb')
city_set = list()
for line in fp.readlines():
city_set.append(str(line.strip(),encoding='utf-8'))
return city_set
month_set = get_month_set()
city_set = get_city_set()
for city in city_set:
file_name = city + '.csv'
fp = open(file_name, 'w')
fp.write('%s,%s,%s,%s,%s,%s,%s,%s,%s\n'%('date','AQI','grade','PM25','PM10','SO2','CO','NO2','O3_8h'))#表頭
for i in range(len(month_set)):
str_month = month_set[i]
weburl = ('%s%s&month=%s' % (base_url, parse.quote(city), str_month))
driver.get(weburl)
dfs = pd.read_html(driver.page_source,header=0)[0]
time.sleep(1)#防止頁面一帶而過,爬不到內容
for j in range(0,len(dfs)):
date = dfs.iloc[j,0]
aqi = dfs.iloc[j,1]
grade = dfs.iloc[j,2]
pm25 = dfs.iloc[j,3]
pm10 = dfs.iloc[j,4]
so2 = dfs.iloc[j,5]
co = dfs.iloc[j,6]
no2 = dfs.iloc[j,7]
o3 = dfs.iloc[j,8]
fp.write(('%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (date,aqi,grade,pm25,pm10,so2,co,no2,o3)))
print('%d---%s,%s---DONE' % (city_set.index(city), city, str_month))
fp.close()
driver.quit()
print ('爬蟲已經爬完!請檢測!')
參考:
杜雨大神python教程
最初版本爬蟲程式碼,已失效
https://github.com/Yinghsusong/aqistudy