[問題] RSelenium 爬蟲迴圈問題

看板R_Language作者時間6年前 (2018/12/10 11:06), 編輯推噓0(004)
留言4則, 2人參與, 6年前最新討論串1/1
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題敘述]: 想要各個節點分別存取,因此利用迴圈的方式,但會出錯,不知問題點在哪。 [程式範例]: title = web.elem$findChildElement(using = "css selector", value = "th:nth-child(i)") 上面這行可以順利執行。 但寫成迴圈如下,就會出錯。 for(i in 2:25) { title = web.elem$findChildElement(using = "css selector", value = "th:nth-child(i)") result[1,i] = title$getElementText() } 會出現錯誤如下: Selenium message:invalid selector: An invalid or illegal selector was specified [環境敘述]: 請提供 sessionInfo() 的輸出結果, 裡面含有所有你使用的作業系統、R 的版本和套件版本資訊, 讓版友更容易找出錯誤 [關鍵字]: 選擇性,也許未來有用 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.177.142 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1544411189.A.F5D.html

12/10 11:28, 6年前 , 1F
"th:nth-child(i)" 中的 i 還只是字串而不是變動的數字
12/10 11:28, 1F

12/10 11:29, 6年前 , 2F
所以value的值要針對i在變動而預先產生正確的字串
12/10 11:29, 2F

12/10 11:32, 6年前 , 3F
例如value=paste0("th:nth-child(", i, ")")之類的
12/10 11:32, 3F

12/10 12:02, 6年前 , 4F
成功了!感恩!
12/10 12:02, 4F
文章代碼(AID): #1S3TWrzT (R_Language)
文章代碼(AID): #1S3TWrzT (R_Language)