---
# System prepended metadata

title: Nov.3 讀取需要帳號密碼登入
tags: [資料科學自學園]

---

# Nov.3 讀取需要帳號密碼登入
###### tags: `資料科學自學園`
## reference
https://weitinglin.com/2017/01/19/%E9%80%B2%E9%9A%8Er%E7%B6%B2%E8%B7%AF%E7%88%AC%E8%9F%B2%E5%88%A9%E7%94%A8rvest%E4%BE%86%E7%88%AC%E5%8F%96%E9%9C%80%E8%A6%81%E5%B8%B3%E8%99%9F%E5%AF%86%E7%A2%BC%E7%99%BB%E5%85%A5%E7%9A%84%E8%97%A5/

## 目前成果
```{r}
#創立網址
#使用一個已經登入過的url頁面
#url.address <- "https://extranet.ohchr.org/sites/hrc/Pages/default.aspx"  #不重要
url.address_1 <- "https://extranet.ohchr.org/_layouts/OHCHR.CustomLogin/OHCHRLogin.aspx?ReturnUrl=%2fsites%2fhrc%2f_layouts%2fAuthenticate.aspx%3fSource%3d%252Fsites%252Fhrc%252FPages%252Fdefault%252Easpx&Source=%2Fsites%2Fhrc%2FPages%2Fdefault%2Easpx"
library(rvest)
library(xml2)
#pgsession <-html_session(url.address)   #不重要
pgsession_1 <-html_session(url.address_1)

#pgform <- html_form(pgsession)[[1]]   #不重要
pgform_1 <- html_form(pgsession_1)[[1]]


filled_form <- set_values(pgform_1,
                          `ctl00$PlaceHolderMain$signInControl$UserName` = "hrc extranet",
                          `ctl00$PlaceHolderMain$signInControl$password` = "1session")


## 送出帳密到這個session中，取得進一步爬取資料的授權
submit_form(pgsession_1,filled_form)


## 接者要跳轉入自己真正有興趣的網頁
function(page
rs.url.address <- paste0("https://extranet.ohchr.org/sites/hrc/Pages/HRCSearch.aspx?k=China&cs=This%20Site&u=https%3A%2F%2Fextranet%2Eohchr%2Eorg%2Fsites%2Fhrc&start1="
                         ,1,"1")        #因為網頁規律為"...."+"頁碼"+"1"
rs.url.address
```