2016年7月18日 星期一
R 語言的一些網路資料
Ref : R語言
存取dt 裡面的資料
dt$Science -> dt 裡面 Science 變數
dt[, 5] -> dt 裡面第五欄的資料
attach(dt) -> 可使dt裡面所有的資料傳到表層
dt[3,] -> 取得dt 裡面第三列的資料
dt[3(3,6),] -> 取得dt 裡面第三列和第六列的資料
subset(dt,Gender=="m") 取得 Gender 為 m 的資料
subset(dt,Science>=60) 取得 Science 大於等於60 的資料
讀取 excel 的檔案...使用 xlsx 的套件
排序資料 -> order() 和 sort()
描述性統計 :
length(變數) # 個數
mean (變數) # 平均數
sd(變數) # 標準差
quantile(變數) # 百分位數
例子:
mean(dt$Science) -> 70.77778
sd(dt$Literature) -> 19.7428
分組之描述性統計
tapply(變數, 分組因子, 運算函數,..)
tapply(dt$Science, dt$Gender, mean)
f m
64.40 78.75
或是用 subset 切出子集合
mean(subset(dt,Gender=="m")$Science)
mean(subset(dt,Gender=="f")$Science)
存取dt 裡面的資料
dt$Science -> dt 裡面 Science 變數
dt[, 5] -> dt 裡面第五欄的資料
attach(dt) -> 可使dt裡面所有的資料傳到表層
dt[3,] -> 取得dt 裡面第三列的資料
dt[3(3,6),] -> 取得dt 裡面第三列和第六列的資料
subset(dt,Gender=="m") 取得 Gender 為 m 的資料
subset(dt,Science>=60) 取得 Science 大於等於60 的資料
讀取 excel 的檔案...使用 xlsx 的套件
排序資料 -> order() 和 sort()
描述性統計 :
length(變數) # 個數
mean (變數) # 平均數
sd(變數) # 標準差
quantile(變數) # 百分位數
例子:
mean(dt$Science) -> 70.77778
sd(dt$Literature) -> 19.7428
分組之描述性統計
tapply(變數, 分組因子, 運算函數,..)
tapply(dt$Science, dt$Gender, mean)
f m
64.40 78.75
或是用 subset 切出子集合
mean(subset(dt,Gender=="m")$Science)
mean(subset(dt,Gender=="f")$Science)
如何使用Python 套件: BeautifulSoup4 剖析網頁內容?
Ref : http://www.largitdata.com/course/8/
from bs4 import BeautifulSoup
html_sample = ' \
<html> \
<body> \
<h1 id="title">Hello World</h1> \
<a href="#" class="link">This is link1</a> \
<a href="# link2" class="link">This is link2</a> \
</body> \
</html>'
soup = BeautifulSoup(html_sample,"html.parser")
#print soup.text
print soup.contents
print soup.select("html")
print soup.select(")
from bs4 import BeautifulSoup
html_sample = ' \
<html> \
<body> \
<h1 id="title">Hello World</h1> \
<a href="#" class="link">This is link1</a> \
<a href="# link2" class="link">This is link2</a> \
</body> \
</html>'
soup = BeautifulSoup(html_sample,"html.parser")
#print soup.text
print soup.contents
print soup.select("html")
print soup.select(")
如何使用POST 抓取網頁內容?
Ref : http://www.largitdata.com/course/10/
import requests
import sys
payload = {
'StartStation':'977abb69-413a-4ccf-a109-0272c24fd490',
'EndStation':'3301e395-46b8-47aa-aa37-139e15708779',
'SearchDate':'2016/07/18',
'SearchTime':'22:30',
'SearchWay':'DepartureInMandarin'
}
res = requests.post("https://www.thsrc.com.tw/tw/TimeTable/SearchResult" , data = payload)
#print(res.text.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))
print(res.text)
如果只是用 print(res.text) 會遇到這個問題:
查了一下google...
Ref : http://marsray.pixnet.net/blog/post/61040521-%5Bpython3%5D-%E7%94%A8-python3-%E5%AF%AB%E4%B8%80%E5%80%8B%E7%B6%B2%E8%B7%AF%E7%88%AC%E8%9F%B2
有兩種解法...但適用第二種結法比較好
print(res.text.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))
就可以正確的抓到網路的資料了
import requests
import sys
payload = {
'StartStation':'977abb69-413a-4ccf-a109-0272c24fd490',
'EndStation':'3301e395-46b8-47aa-aa37-139e15708779',
'SearchDate':'2016/07/18',
'SearchTime':'22:30',
'SearchWay':'DepartureInMandarin'
}
res = requests.post("https://www.thsrc.com.tw/tw/TimeTable/SearchResult" , data = payload)
#print(res.text.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))
print(res.text)
如果只是用 print(res.text) 會遇到這個問題:
查了一下google...
Ref : http://marsray.pixnet.net/blog/post/61040521-%5Bpython3%5D-%E7%94%A8-python3-%E5%AF%AB%E4%B8%80%E5%80%8B%E7%B6%B2%E8%B7%AF%E7%88%AC%E8%9F%B2
有兩種解法...但適用第二種結法比較好
print(res.text.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))
就可以正確的抓到網路的資料了
訂閱:
意見 (Atom)


