レンダリングがIEになるようで、IEのバージョン変更はレジストリからのみ。
毎回、DocumentCompletedが走るので無限ループになってしまったりするので注意。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.ScriptErrorsSuppressed = true; webBrowser1.Navigate("https://weather.yahoo.co.jp/weather/jp/xxx"); webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(tenki); } private void tenki(object sender, WebBrowserDocumentCompletedEventArgs e) { HtmlElementCollection hec = webBrowser1.Document.All; foreach (HtmlElement he in hec) { if (he.GetAttribute("className").ToString() == "forecastCity") { /* 以下のように直接CSSを書いても大丈夫 webBrowser1.DocumentText = "<style>" + "*{font: normal 12px;}" + "th,tr,td{padding: 10px; border: 1px solid #ccc;}" + "table{border-collapse: collapse; text-align: center}" + "</style>" + "<table>" + he.InnerHtml + "</table>"; */ webBrowser1.DocumentText = "<html><head>" + "<link rel='stylesheet' href='//s.yimg.jp/images/weather/pc/v2/css/weatherCommon-2.1.0.css'>" + "<link href='https://s.yimg.jp/yui/jp/mh/pc/1.4.8/css/std.css' rel='stylesheet' type='text/css' media='all'>" + "</head>" + "<body><div>" + he.InnerHtml + "</div></body></html>"; } } } } } |
こんな感じで読み込むことも可能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Boolean isLoad = false; public Form1() { InitializeComponent(); webBrowser1.ScriptErrorsSuppressed = true; webBrowser1.Navigate("xxx"); webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LoadPage); timer1.Start(); } private void LoadPage(object sender, WebBrowserDocumentCompletedEventArgs e) { if (isLoad== true) return; HtmlElementCollection hec = webBrowser1.Document.Body.All; string body = ""; //class foreach(HtmlElement he in hec) { if(he.GetAttribute("className").ToString() == "xxx") { body += he.InnerHtml; } } /* ID foreach (HtmlElement he in hec) { if (he.Id == "xxx") body += he.InnerHtml; } */ webBrowser1.Document.Body.InnerHtml = body; isLoad = true; } private void timer1_Tick(object sender, EventArgs e) { this.Text = DateTime.Now.ToString(); if (isLoad == true && DateTime.Now.Minute.ToString() == "59" && DateTime.Now.Second.ToString() == "59") isLoad = false; if (isLoad == false && DateTime.Now.Minute.ToString() == "1" && DateTime.Now.Second.ToString() == "1") webBrowser1.Refresh(); } } } |