
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !
------------------------------------------------------------------------------------------------------
using System;using System.Drawing;using System.Text;using NSoup;using NSoup.Nodes;using System.IO;using System.Net;using System.Text.RegularExpressions;using System.Windows.Forms;namespace Pneumonia{ public partial class MainForm : DevComponents.DotNetBar.OfficeForm { static string confirmedCount, suspectedCount, deadCount, curedCount, updateTime,dataUpdateTime; static string url = "https://3g.dxy.cn/newh5/view/pneumonia"; static int count = 0; static Document doc; public MainForm(){ this.EnableGlass = false; InitializeComponent(); this.SizeChanged += new Resize(this).Form1_Resize; //窗口自适应代码 } private void MainForm_Load(object sender, EventArgs e){ timer1.Enabled = true; timer1.Interval = 1; timer1.Start(); WebClient wc = new WebClient(); byte[] htmlData = wc.DownloadData(url); string html = Encoding.UTF8.GetString(htmlData); logWrite(html);//将网页内容写入txt文件,以方便查看 toolStripStatusLabel1.Text = DateTime.Now.ToString(); } private void timer1_Tick(object sender, EventArgs e){ dataUpdateTime = DateTime.Now.ToString(); count++; timer1.Interval = 300000; GetData(); lbl1.Text = "☛ 病毒: " + regularMatchStr("getStatisticsService", "virus\":\"(.+?)\","); lbl2.Text = "☛ 传染源: " + regularMatchStr("getStatisticsService", "infectSource\":\"(.+?)\","); lbl3.Text = "☛ 传播途径: " + regularMatchStr("getStatisticsService", "passWay\":\"(.+?)\","); lbl4.Text ="☛ "+regularMatchStr("getStatisticsService", "remark1\":\"(.+?)\","); lbl5.Text ="☛ "+regularMatchStr("getStatisticsService", "remark2\":\"(.+?)\","); Image map =UrlToImage("https://img1.dxycdn.com/2020/0201/450/3394153392393266839-135.png"); pictureBox1.Image = map; Image chart = UrlToImage("https://img1.dxycdn.com/2020/0201/693/3394145745204021706-135.png"); pictureBox2.Image = chart; updateTimeLbl.Text = "截至 " + updateTime + " 全国数据统计"; confirmedLbl.Text = confirmedCount; suspectedLbl.Text = suspectedCount; deadLbl.Text = deadCount; curedLbl.Text = curedCount; } public static void GetData(){ //直接通过url来获取Document对象 doc = NSoupClient.Connect(url).Get(); //先获取id为artContent的元素,再获取所有的p标签 updateTime = ConvertStringToDateTime(regularMatchStr("getStatisticsService", "modifyTime\":(.+?),")).ToString(); confirmedCount = regularMatchStr("getStatisticsService", "confirmedCount\":(.+?),"); suspectedCount = regularMatchStr("getStatisticsService", "suspectedCount\":(.+?),"); deadCount = regularMatchStr("getStatisticsService", "deadCount\":(.+?),"); curedCount = regularMatchStr("getStatisticsService", "curedCount\":(.+?),"); } #region 下载图片到Image public static Image UrlToImage(string url){ WebClient mywebclient = new WebClient(); byte[] Bytes = mywebclient.DownloadData(url); using (MemoryStream ms = new MemoryStream(Bytes)) { Image outputImg = Image.FromStream(ms); return outputImg; } }
No comments:
Post a Comment