iT邦幫忙

0

請教使用C#抓取銀行匯率問題

  • 分享至 

  • xImage

各位大大好:
小弟近期研究C# WinForm透過WebBrowser抓取ajax動態銀行匯率(網址
但都抓不到匯率(如下圖),求救高手能否教小弟如何抓取此網頁匯率? 或是C#需搭配其他Tools? 感謝您~
https://ithelp.ithome.com.tw/upload/images/20200315/20110817BUNEZegHJ0.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
小魚
iT邦大師 1 級 ‧ 2020-03-16 13:52:42
最佳解答

你那個應該是沒有載入js執行後的結果,
可以考慮selenium.

cnews iT邦新手 5 級 ‧ 2020-03-17 23:25:14 檢舉

小魚~
謝謝您的寶貴意見,我也來試看看,感謝您~

cnews iT邦新手 5 級 ‧ 2020-04-10 22:25:27 檢舉

小魚~
感謝您的指點,上個月用您說的方法解決了,非常感謝您 ^_^b

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-03-15 19:02:32

既然是 ajax,就直接取得「資料」的部份就好
去剖析「網頁 HTML」的部份
既麻煩又是吃力不討好

直接取得資料的方式如下述

URL:https://www.megabank.com.tw/api/services/ExchangeServices.aspx/CallAPI
方法:POST
參數:{"args":"{\"service_name\":\"ex_rate_new\",\"args\":{}}"}
回覆:JSON 格式資料
d: {"ex_rate_new":{"last_update":"2020/03/15 18:52:19","args":"","curr_names":[{"curr_code":"01","curr_name":"USD","curr_cname":"美元"},{"curr_code":"03","curr_name":"HKD","curr_cname":"港幣"},{"curr_code":"04","curr_name":"GBP","curr_cname":"英鎊"},{"curr_code":"05","curr_name":"AUD","curr_cname":"澳幣"},{"curr_code":"06","curr_name":"SGD","curr_cname":"新加坡幣"},{"curr_code":"07","curr_name":"CHF","curr_cname":"瑞士法郎"},{"curr_code":"08","curr_name":"JPY","curr_cname":"日圓"},{"curr_code":"09","curr_name":"CAD","curr_cname":"加拿大幣"},{"curr_code":"12","curr_name":"SEK","curr_cname":"瑞典幣"},{"curr_code":"13","curr_name":"KRW","curr_cname":"韓幣"},{"curr_code":"14","curr_name":"MYR","curr_cname":"馬來幣"},{"curr_code":"15","curr_name":"IDR","curr_cname":"印尼幣"},{"curr_code":"17","curr_name":"THB","curr_cname":"泰幣"},{"curr_code":"21","curr_name":"PHP","curr_cname":"菲律賓幣"},{"curr_code":"24","curr_name":"NZD","curr_cname":"紐西蘭幣"},{"curr_code":"27","curr_name":"ZAR","curr_cname":"南非幣"},{"curr_code":"28","curr_name":"MOP","curr_cname":"澳門幣"},{"curr_code":"37","curr_name":"EUR","curr_cname":"歐元"},{"curr_code":"38","curr_name":"VND","curr_cname":"越南幣"},{"curr_code":"39","curr_name":"CNY","curr_cname":"人民幣"}],"currs":[{"curr_code":"01","curr_name":"USD","curr_cname":"美元","rates":{"bidlistrate":"30.1400","cashbidlistrate":"29.8000","asklistrate":"30.2400","cashasklistrate":"30.4700"}},{"curr_code":"03","curr_name":"HKD","curr_cname":"港幣","rates":{"bidlistrate":"3.8590","cashbidlistrate":"3.7290","asklistrate":"3.9190","cashasklistrate":"3.9350"}},{"curr_code":"04","curr_name":"GBP","curr_cname":"英鎊","rates":{"bidlistrate":"36.8700","cashbidlistrate":"35.8400","asklistrate":"37.2700","cashasklistrate":"37.9800"}},{"curr_code":"08","curr_name":"JPY","curr_cname":"日圓","rates":{"bidlistrate":"0.2780","cashbidlistrate":"0.2708","asklistrate":"0.2821","cashasklistrate":"0.2835"}},{"curr_code":"05","curr_name":"AUD","curr_cname":"澳幣","rates":{"bidlistrate":"18.5600","cashbidlistrate":"18.2800","asklistrate":"18.8000","cashasklistrate":"19.0700"}},{"curr_code":"09","curr_name":"CAD","curr_cname":"加拿大幣","rates":{"bidlistrate":"21.7900","cashbidlistrate":"21.4100","asklistrate":"21.9900","cashasklistrate":"22.3200"}},{"curr_code":"06","curr_name":"SGD","curr_cname":"新加坡幣","rates":{"bidlistrate":"21.2600","cashbidlistrate":"20.7700","asklistrate":"21.4400","cashasklistrate":"21.6600"}},{"curr_code":"27","curr_name":"ZAR","curr_cname":"南非幣","rates":{"bidlistrate":"1.8100","cashbidlistrate":"","asklistrate":"1.9100","cashasklistrate":""}},{"curr_code":"12","curr_name":"SEK","curr_cname":"瑞典幣","rates":{"bidlistrate":"3.0600","cashbidlistrate":"","asklistrate":"3.1600","cashasklistrate":""}},{"curr_code":"07","curr_name":"CHF","curr_cname":"瑞士法郎","rates":{"bidlistrate":"31.7100","cashbidlistrate":"31.0000","asklistrate":"31.9100","cashasklistrate":"32.1900"}},{"curr_code":"17","curr_name":"THB","curr_cname":"泰幣","rates":{"bidlistrate":"0.9290","cashbidlistrate":"0.8190","asklistrate":"0.9710","cashasklistrate":"1.0090"}},{"curr_code":"24","curr_name":"NZD","curr_cname":"紐西蘭幣","rates":{"bidlistrate":"18.2100","cashbidlistrate":"17.8200","asklistrate":"18.4100","cashasklistrate":"18.6900"}},{"curr_code":"37","curr_name":"EUR","curr_cname":"歐元","rates":{"bidlistrate":"33.3500","cashbidlistrate":"32.7300","asklistrate":"33.7500","cashasklistrate":"34.0700"}},{"curr_code":"13","curr_name":"KRW","curr_cname":"韓幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0232","asklistrate":"","cashasklistrate":"0.0269"}},{"curr_code":"14","curr_name":"MYR","curr_cname":"馬來幣","rates":{"bidlistrate":"","cashbidlistrate":"5.9990","asklistrate":"","cashasklistrate":"7.5480"}},{"curr_code":"15","curr_name":"IDR","curr_cname":"印尼幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0017","asklistrate":"","cashasklistrate":"0.0024"}},{"curr_code":"21","curr_name":"PHP","curr_cname":"菲律賓幣","rates":{"bidlistrate":"","cashbidlistrate":"0.5500","asklistrate":"","cashasklistrate":"0.6630"}},{"curr_code":"28","curr_name":"MOP","curr_cname":"澳門幣","rates":{"bidlistrate":"","cashbidlistrate":"3.5080","asklistrate":"","cashasklistrate":"3.9240"}},{"curr_code":"38","curr_name":"VND","curr_cname":"越南幣","rates":{"bidlistrate":"","cashbidlistrate":"0.0011","asklistrate":"","cashasklistrate":"0.0015"}},{"curr_code":"39","curr_name":"CNY","curr_cname":"人民幣","rates":{"bidlistrate":"4.2750","cashbidlistrate":"4.2050","asklistrate":"4.3250","cashasklistrate":"4.3670"}}]}}
看更多先前的回應...收起先前的回應...
淺水員 iT邦大師 6 級 ‧ 2020-03-17 03:38:02 檢舉

header 要加上:Content-Type: application/json; charset=utf-8

cnews iT邦新手 5 級 ‧ 2020-03-17 23:23:38 檢舉

淺水員 & 海綿寶寶:
非常感謝您們的回覆與協助,我來試用您們說的方法看看,3Q~

cnews iT邦新手 5 級 ‧ 2020-03-19 10:51:50 檢舉

各位高手們好:
目前測試預到1個問題,抓不出跟海綿寶寶一樣的資料,程式到using (WebResponse response = request.GetResponse()),這行就出錯了,不知問題出在哪?,請各位高手們指點一下小弟,感激不盡~ 謝謝。

試試看用 HTTPClient
2019 年的範例

cnews iT邦新手 5 級 ‧ 2020-03-19 13:47:19 檢舉

海綿寶寶您好:
感謝您的指導~ 參考"2019 年的範例"程式後,我嘗試用HTTPClient做了個測試程式(如下),不過會出現StatusCode: 500, ReasonPhrase: 'Internal Server Error'...等訊息,不知JsonString是否設錯,請您再指點一下,感謝您~。
目前無法直接設成string JsonString = {"args":"{"service_name":"ex_rate_new","args":{}}"} 會出錯,所以改成 @"args"":{""service_name"":""ex_rate_new"",""args"":{}}"

我要發表回答

立即登入回答