咱們進入天氣網首頁:http://lishi.tianqi.com/,以廣州2017年09月曆史天氣爲例,把當前頁面切換到廣州天氣頁面,以下圖:html
繼續往下滾動頁面,直到看到廣州歷史天氣詳情,而後點擊2017年09月天氣 ,進入廣州2017年09月天氣詳情頁面,以下圖:node
注意連接地址的變化:連接中的guangzhou表明地區,201709表明時間。也就是說咱們能夠經過控制這兩個參數,跳轉到任意地區和時間的歷史天氣情況頁面。
如今咱們看下廣州地區201709[http://lishi.tianqi.com/guang...頁面的詳細信息,滾動頁面,能夠找到咱們想要爬取的數據:chrome
如今,咱們已經有了大概的思路:瀏覽器
不難看出,咱們的重點是如何解決第一個問題。只要把第一問中的方法包裝成函數,就能夠經過調節參數解決第二個問題。函數
咱們須要在瀏覽器中調試js代碼,chrome中按F12便可查看。經過搜索關鍵字:日期,最高氣溫 ...任何一個都行,獲得如下信息。能夠發現咱們所須要的數據都在<div class="tqtongji2">盒子中,而每一行數據又都在ul中。因此,咱們能夠經過這兩個特徵來提取數據。fetch
獲取<div class="tqtongji2">以及ul中內容的R代碼以下:spa
library(rvest) library(plyr) city <- 'guangzhou' date <- '201709' baseUrl <- 'http://lishi.tianqi.com/' Url <- paste(baseUrl, city, '/', date, '.html', sep = '') content <- Url %>% read_html(encoding='GBK') %>% html_nodes('div.tqtongji2') %>% html_nodes("ul") %>% html_text() head(content)
content的內容以下:3d
[1] "日期\r\n\t\t 最高氣溫\r\n\t\t 最低氣溫\r\n\t\t 天氣\r\n\t\t 風向\r\n\t\t 風力\r\n\t\t " [2] "2017-09-01\r\n\t\t \t\t\t \t35\r\n\t\t \t26\r\n\t\t \t雷陣雨\r\n\t\t \t東北風\r\n\t\t \t1級\r\n \t " [3] "2017-09-02\r\n\t\t \t\t\t \t35\r\n\t\t \t26\r\n\t\t \t雷陣雨\r\n\t\t \t東風\r\n\t\t \t1級\r\n \t " [4] "2017-09-03\r\n\t\t \t\t\t \t34\r\n\t\t \t25\r\n\t\t \t雷陣雨\r\n\t\t \t東北風\r\n\t\t \t2級\r\n \t " [5] "2017-09-04\r\n\t\t \t\t\t \t30\r\n\t\t \t25\r\n\t\t \t中雨\r\n\t\t \t東南風\r\n\t\t \t微風\r\n \t "
咱們能夠發現數據之間用rntt之類的隔開。而'rntt'這些裏面有:回車符,換行符和製表符。他們的共同點就是全都是空格。因此咱們能夠經過空格來進行分列,提取相應的數據。調試
content <- content %>% strsplit("\\s{4,}") content
輸出結果爲列表的形式:code
[[1]] [1] "日期" "最高氣溫" "最低氣溫" "天氣" "風向" "風力" [[2]] [1] "2017-09-01" "35" "26" "雷陣雨" "東北風" "1級" [[3]] [1] "2017-09-02" "35" "26" "雷陣雨" "東風" "1級" [[4]] [1] "2017-09-03" "34" "25" "雷陣雨" "東北風" "2級"
爲了美觀和方便操做,咱們把它轉換爲數據框的形式:
content <- ldply(content[-1]) names(content) <- c('date', 'highDegree', 'lowDegree', 'weather', 'windDirection', 'windForce')
至此,咱們已經成功獲取這個頁面中的數據:
date highDegree lowDegree weather windDirection windForce 1 2017-09-01 35 26 雷陣雨 東北風 1級 2 2017-09-02 35 26 雷陣雨 東風 1級 3 2017-09-03 34 25 雷陣雨 東北風 2級 4 2017-09-04 30 25 中雨 東南風 微風 5 2017-09-05 33 26 雷陣雨 東風 1級 6 2017-09-06 33 26 雷陣雨 南風 1級
咱們將上述中的代碼,進行整理,封裝成一個函數,以下:
library(rvest) library(data.table) library(plyr) fetchData <- function(city, date){ baseUrl <- 'http://lishi.tianqi.com/' Url <- paste(baseUrl, city, '/', date, '.html', sep = '') content <- Url %>% read_html(encoding='GBK') %>% html_nodes('div.tqtongji2') %>% html_nodes("ul") %>% html_text() %>% strsplit("\\s{4,}") content <- ldply(content[-1]) names(content) <- c('date', 'highDegree', 'lowDegree', 'weather', 'windDirection', 'windForce') return(content) }
這時候,咱們能夠調用上述函數,並經過傳遞地區city和時間date參數來爬取相應的數據。例如,爬取北京地區2017年09月的歷史天氣:
beijing <- fetchData(city = 'beijing', date = '201709') head(beijing)
date highDegree lowDegree weather windDirection windForce 1 2017-09-01 27 19 多雲 西南風 2級 2 2017-09-02 27 19 多雲 西南風 2級 3 2017-09-03 29 19 多雲 南風 2級 4 2017-09-04 28 20 陰 南風 2級 5 2017-09-05 30 18 多雲 西北風 2級 6 2017-09-06 31 18 晴 西南風 2級
固然若是你想爬取北京地區2016年的整年天氣情況,能夠利用上面的函數,經過循環,合併獲得你想要的數據。