Use regular expressions to convert html webpage data into Web Services

Source: Internet
Author: User

This question is very simple, the Bank of China has a page to check the exchange rate of the day (http://www.bank-of-china.com/info/qpindex.shtml), but is the traditional Html format, and it does not provide Xml format or WebService query. If you want other information systems to read the data at any time, it is convenient that the Bank of China provides a WebService Interface for you to call. This is also a typical secure WebService application. Unfortunately, the Bank of China did not do this. Can we do it ourselves? Of course, as long as you use a program to analyze its html webpage, you can easily read the data. Text Analysis, of course, depends on our "Regular Expression" (haha, in fact, this is the real purpose of writing this program-apply Regular expressions .)

The BOC page is similar:

Date:Valid Period: 2004/09/30 to 2004/10/07

Currency name Current remittance purchase price Cash purchase price Sell bid Base Price
Pound

1488.1700

1453.1500

1492.6400

Hong Kong dollar

105.9700

105.3300

106.2900

106.1100

USD

826.4200

821.4500

828.9000

827.6600

Swiss franc

655.9300

641.1400

659.2200

Singapore dollar

488.7600

477.2600

490.2300

Swedish krona

112.4900

109.8400

112.8300

Danish krona

136.5900

133.3700

137.0000

Norwegian Krona

121.9500

119.0800

122.3100

Yen

7.4344

7.3785

7.4717

7.4519

Canadian dollar

650.8000

635.4800

652.7600

Australian dollar

591.9900

578.6400

594.9600

Euro

1019.6400

1010.9600

1022.7000

1019.7000

Macao dollar

103.2200

102.6000

103.5300

Philippine peso

14.6700

14.3300

14.7200

19.9000

19.4300

19.9600

New Zealand dollar

553.7000

555.3600

After the code analysis, a regular expression is provided. Of course, this expression is not perfect yet, but it is still correct for the exchange rate page of the relatively fixed bank of china.

@ "<Tr bgcolor = '#/w +'> <td height = '20'> (? <Currency>. *) </td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Bankbuytt>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Buynotes>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Strong>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Base>/d *.? /D *) (& nbsp) + .? </Td>/s *"

Then it is very easy to filter. I always thought that code is the best explanation, especially for elegant languages, because I will not talk much about it, and the code is waiting for me.

This is the code for ForeignExchange. asmx on the created WebService page:

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. Web;
Using System. Net;
Using System. Web. Services;
Using System. Xml;
Using System. Text;
Using System. Text. RegularExpressions;
Using System. IO;

Namespace ChinaBank
{
/// <Summary>
/// Summary description for ForeignExchange.
/// </Summary>
[WebService (Namespace = "http://dancefires.com/ChinaBank/")]
Public class foreignexchange: system. Web. Services. WebService
{
Public foreignexchange ()
{
// Codegen: This call is required by the ASP. NET web services designer
Initializecomponent ();
}

# Region component designer generated code

// Required by the Web Services designer
Private IContainer components = null;

/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
}

/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing & components! = Null)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

[Webmethod]
Public xmldatadocument getforeignexchangerates ()
{
Return getxmldoc ();
}
[Webmethod]
Public dataset getforeignexchangeratesdataset ()
{
Return getXmlDoc (). DataSet;
}
[WebMethod]
Public string GetBankPage ()
{
Return getWebContent ("http://www.bank-of-china.com/info/whjrpj.html ");
}
// Private methods
Private string getWebContent (string url)
{
Using (WebClient client = new WebClient ())
{
Byte [] buffer = client. downloaddata (URL );
String STR = encoding. getencoding ("gb2312"). getstring (buffer, 0, buffer. Length );
Return STR;
}
}
Private xmldatadocument getxmldoc ()
{
String webcontent = getWebContent ("http://www.bank-of-china.com/info/whjrpj.html ");

// Prepair for DataSet
DataSet ds = new DataSet ("Exchange ");
DataTable dt = new DataTable ("ForeignExchange ");
Ds. Tables. Add (dt );
Dt. Columns. Add ("Currency", typeof (string ));
Dt. Columns. Add ("BankBuyTT", typeof (double ));
Dt. Columns. Add ("BankBuyNotes", typeof (double ));
Dt. Columns. Add ("banknames", typeof (double ));
Dt. Columns. Add ("Baseline", typeof (double ));
XmlDataDocument xmldoc = new XmlDataDocument (ds );

Regex expr = new Regex (
@ "<Tr bgcolor = '#/w +'> <td height = '20'> (? <Currency>. *) </td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Bankbuytt>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Buynotes>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Strong>/d *.? /D *) (& nbsp) + .? </Td>/s * "+
@ "<Td height = '20'> <p align = 'right'> (? <Base>/d *.? /D *) (& nbsp) + .? </Td>/s *"
, RegexOptions. Compiled );
For (Match m = expr. Match (webcontent); m. Success; m = m. NextMatch ())
{
String key;
DataRow row = dt. NewRow ();
Row ["Currency"] = m. Groups ["currency"];
Key = m. Groups ["bankbuytt"]. ToString ();
Row ["bankbuytt"] = key. length> 0? Convert. todouble (key)/100: 0;
Key = M. Groups ["buynotes"]. tostring ();
Row ["bankbuynotes"] = key. length> 0? Convert. todouble (key)/100: 0;
Key = M. Groups ["success"]. tostring ();
Row ["bankdetail"] = key. length> 0? Convert. todouble (key)/100: 0;
Key = M. Groups ["base"]. tostring ();
Row ["Baseline"] = key. Length> 0? Convert. ToDouble (key)/100: 0;
Dt. Rows. Add (row );
}
Return xmldoc;
}
}
}

The client is also very easy, as long as the corresponding WebService Proxy is generated using wsdl, it can be called directly, because I asked the Server to return DataSet, therefore, the client can directly use the DataGrid to display the DataSet, which is very Easy. On this issue, the client has no technical key points.

Using System;
Using System. Threading;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;

Namespace BankDataClient
{
/// <Summary>
/// Summary description for frmMainBankRates.
/// </Summary>
Public class frmMainBankRates: System. Windows. Forms. Form
{
Private System. Windows. Forms. DataGrid dataGrid1;
Private System. Windows. Forms. Button btnConnect;
Private System. Data. DataSet ds;
Private BankDataClient.com. dancefires. [url] www. ForeignExchange [/url] proxy = new BankDataClient.com. dancefires. www. ForeignExchange ();
Private System. Windows. Forms. TextBox txtUrl;
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public frmMainBankRates ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
Try
{
TxtUrl. Text = System. Configuration. ConfigurationSettings. deleettings ["url"];
Proxy. Url = txtUrl. Text;
}
Catch (Exception)
{
Proxy. Url = "http://www.dancefires.com/ChinaBank/ForeignExchange.asmx ";
Txturl. Text = proxy. url;
}
}

/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region windows Form Designer generated code
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
This. Maid = new System. Windows. Forms. DataGrid ();
This. ds = new System. Data. DataSet ();
This. btnConnect = new System. Windows. Forms. Button ();
This.txt Url = new System. Windows. Forms. TextBox ();
(System. ComponentModel. ISupportInitialize) (this. dataGrid1). BeginInit ();
(System. ComponentModel. ISupportInitialize) (this. ds). BeginInit ();
This. SuspendLayout ();
//
// DataGrid1
//
This. dataGrid1.DataMember = "";
This. dataGrid1.DataSource = this. ds;
This. dataGrid1.HeaderForeColor = System. Drawing. SystemColors. ControlText;
This. Maid = new System. Drawing. Point (32, 48 );
This. Maid = "maid ";
This. dataGrid1.Size = new System. Drawing. Size (480,256 );
This. Maid = 0;
//
// Ds
//
This. ds. DataSetName = "Exchange ";
This. ds. Locale = new System. Globalization. CultureInfo ("zh-CN ");
//
// BtnConnect
//
This. btnConnect. Location = new System. Drawing. Point (432, 16 );
This. btnconnect. Name = "btnconnect ";
This. btnconnect. tabindex = 1;
This. btnconnect. Text = "Connect ";
This. btnconnect. Click + = new system. eventhandler (this. btnconnect_click );
//
// Txturl
//
This.txt URL. Location = new system. Drawing. Point (32, 16 );
This.txt Url. Name = "txtUrl ";
This.txt Url. Size = new System. Drawing. Size (384, 20 );
This.txt Url. TabIndex = 2;
This.txt Url. Text = "";
//
// FrmMainBankRates
//
This. AutoScaleBaseSize = new System. Drawing. Size (5, 13 );
This. ClientSize = new System. Drawing. Size (544,318 );
This.Controls.Add(this.txt Url );
This. Controls. Add (this. btnConnect );
This. Controls. Add (this. dataGrid1 );
This. Name = "frmMainBankRates ";
This. Text = "Foreign Exchange Rates of Bank of China ";
(System. ComponentModel. ISupportInitialize) (this. dataGrid1). EndInit ();
(System. ComponentModel. ISupportInitialize) (this. ds). EndInit ();
This. ResumeLayout (false );

}
# Endregion

Private void btnConnect_Click (object sender, System. EventArgs e)
{
UpdateDataGrid ();
}
Private void UpdateDataGrid ()
{
Try
{
Btnconnect. Enabled = false;
Txturl. readonly = true;
Proxy. url = txturl. text;
DS = proxy. getforeignexchangeratesdataset ();
Datagrid1.setdatabinding (DS, "foreignexchange ");
DataGrid1.Update ();
}
Catch (Exception err)
{
MessageBox. Show (err. Message );
}
Finally
{
TxtUrl. ReadOnly = false;
Btnconnect. Enabled = true;
}
}
[Stathread]
Static void main (string [] ARGs)
{
Application. Run (New frmmainbankrates ());
}
}
}

With this example, you can learn about the most basic XML, WebService, regular expression, dataset, and DataGrid.

All software code and screenshots can be obtained from the following connection:

Http://www.dancefires.com/ChinaBank/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.