精華區beta R_Language 關於我們 聯絡資訊
軟體熟悉度:初學者 問題敘述: 各位大大好,最近找到這個網頁的教學來學爬蟲,本身是商科無程式基礎,但之後業界好 像會用到所以先學習資料科學相關的東西 https://medium.com/chikuwa-tech-study/%E4%BD%BF%E7%94%A8r%E8%AA%9E%E8%A8%80%E9 %80%B2%E8%A1%8C%E7%88%AC%E8%9F%B2-936637f272cb 我按照他的教學點進去裏頭的網頁做做看 https://www.mobile01.com/forumtopic.php?c=29 然後點了第一篇對標題和留言按下檢查之後複製了full xpath 在R裡面打出以下東西: library(rvest) library(dplyr) x=read_html("https://www.mobile01.com/forumtopic.php?c=29") x title_path="/html/body/div[1]/main/div[1]/div/div/div/div[1]/div[7]/div/div[2] /div/div[2]/div[1]/div[1]/div/div/a " titles=x %>% html_nodes(xpath = "title_path") %>% html_text() reply_path="/html/body/div[1]/main/div[1]/div/div/div/div[1]/div[7]/div/div[2] /div/div[2]/div[1]/div[4]/div " replies=x %>% html_nodes(xpath = "reply_path") %>% html_text() my.table=data.frame(title=titles,reply=replies) View(my.table) 語法沒有任何錯誤,但只有跑出一行東西,請問有人知道錯在哪裡嗎 (我跟作者的程式碼幾乎一模一樣,但我的瀏覽器是用估狗的) 另外一個問題,需要會員登入的論壇是不是既無法進行爬蟲呢? (朋友去伊利爬a片爬不出任何東西) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.18.140 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1587985735.A.5ED.html
bboybighead2: 需要登入的網站可以用selenium或送request 04/27 19:34
ladie4: 謝謝b大 我之後會試試看的 那上面這個請問您有頭緒嗎…… 04/27 19:59
bboybighead2: https://i.imgur.com/mMVEgwI.jpg 04/28 00:01
bboybighead2: 可以觀察一下source的結構,再決定用哪些標籤查找比 04/28 00:04
bboybighead2: 較方便。或使用chrome的插件selectorgadget滿方便的 04/28 00:04
bboybighead2: 。 04/28 00:04
bboybighead2: 然後你的問題是出在html_nodes(xpath = "reply_path 04/28 01:00
bboybighead2: ") 這裡的reply_path不需要雙引號,不然就不會傳你 04/28 01:00
bboybighead2: 想要的xpath路徑,而是傳”reply_path”這個字串而 04/28 01:00
bboybighead2: 已 04/28 01:00