iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 30
0
自我挑戰組

When Bioinfo met Julia: Bioinformatician的30天Julia學習之路系列 第 31

[Day 30] 疑?最後一天了,來看看怎麼使用Julia爬蟲好了

最後一天

收集資料也是生物資訊領域也很重要的一環,而且不是每個資料庫網站都很貼心地提供API讓我們很同意地取得資料,故這最後一天姑且讓我們來看看怎麼使用Julia進行爬蟲好了。

所需工具

一個例子

今天我們想要爬一下StackOverflow上面關於julia-lang這個關鍵字的所有問題及連結,另外我想看一下這些問題是否有被回答或討論,以及我還想知道這個問題得到多少票,我們可以這樣做

using HTTP
using Gumbo
using Cascadia
keyword = "julia"
url = "https://stackoverflow.com/questions/tagged/$keyword"

response = HTTP.get(url)
html = parsehtml(String(response.body))
questionsummary = eachmatch(Selector(".question-summary"),html.root)
for qs in questionsummary
    votes = nodeText(eachmatch(Selector(".votes .vote-count-post "), qs)[1])
    answered = length(eachmatch(Selector(".status.answered"), qs)) > 0
    href = eachmatch(Selector(".question-hyperlink"), qs)[1].attributes["href"]
    title = nodeText(eachmatch(Selector(".question-hyperlink"),qs)[1])
    println("$votes  $answered  [$title](http://stackoverflow.com$href)")
end

執行之後,就可以看到結果了:

https://ithelp.ithome.com.tw/upload/images/20181031/20111688mKiIYeITIP.png

是不是還算挺方便的呢?


上一篇
[Day 29] 用Julia來研究單細胞定序?
下一篇
用Julia將一個Genome按照某個window size切並計算GC content
系列文
When Bioinfo met Julia: Bioinformatician的30天Julia學習之路32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
杜岳華
iT邦新手 5 級 ‧ 2018-11-01 23:03:28

喔天阿!我還沒有用過 Julia 爬過網站,可是看起來超級流暢的啊!

也是剛好看到有人在講怎麼用Julia寫RESTful API才想到同樣可以用HTTP.jl的方式,拿來爬蟲。

我要留言

立即登入留言