Use the regular expression to turn the HTML web page data into web service

xiaoxiao2021-03-06  74

This topic is very simple. Bank of China has a website (http://www.bank-of-china.com/info/qpindex.shtml), but it is not available in the traditional HTML format. XML format or WebService query. Now if you want other information systems to read them at any time, it is convenient to provide a WEBSERVICE interface for the WEBSERVICE to provide you call, which is also a typical security WebService application. Unfortunately, the Bank didn't do it, then can we do it yourself? Of course, you can read the data easily using the program to analyze its HTML web page. Text analysis, of course, we have to look at our "regular expression" (Oh, in fact, this is the true purpose of writing this program - Applying regular expressions.)

The pages of the Bank are similar to:

Date: 2004/09/30 Valid until 2004/10/07

Name of the currency cash purchase price in cash Bid Ask base price of £ 1488.1700 1453.1500 1492.6400 Hong Kong dollars 105.9700 105.3300 106.1100 826.4200 821.4500 828.9000 106.2900 US dollars CHF 655.9300 641.1400 659.2200 827.6600 488.7600 477.2600 490.2300 SGD SEK 112.4900 109.8400 112.8300 136.5900 133.3700 137.0000 DKK NOK 121.9500 119.0800 122.3100 yen 7.4344 7.3785 7.4717 7.4519 Australian Dollar Canadian Dollar 650.8000 635.4800 652.7600 591.9900 578.6400 594.9600 euros 1019.6400 1010.9600 1022.7000 1019.7000 patacas 103.2200 102.6000 103.5300 14.6700 14.3300 14.7200 Philippine Peso Thai Baht 19.9000 19.4300 19.9600 553.7000 555.3600 New Zealand Dollar

After the code analysis, a regular expression is given, of course, this expression is not perfect, but there is no problem for the current relatively fixed China Rank.

@ " (? . *) / s *" @ "

(? / D *.? / d *) (& nbsp) .? / s * " @"

(? / s * " @"

? / D *.? / d *) (& nbsp) .? / s * " @"

(? / d *.? / d *) (& nbsp) . "" The filter is very simple. I have always thought that the code is the best explanation, especially for elegant languages, because I don't say much, the code is served. This is the code for the WEBSERVICE page foreignexchange.asmx:

Using system

;

Using system

.

Collectes

;

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 description for foreIgnexchange. ///

[

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 DesignerPrivate ICONTAINER Components

=

NULL

;

///

/// Required Method for Designer Support - Do Not Modify /// The Contents of this method with the code editor. ///

Private vid initializecomponent

() {}

///

/// clean up any resources being used. ///

Protected Override Void Dispose

(

Bool Disposing

) {IF

Disposing

&&&&

Components

! =

NULL

) {

Components

.

Dispose

();

Base

.

Dispose

(

Disposing

}

#ndregion

[

WebMethod

]

Public XMLDATADOCUMENT GETFOREIGNEXCHANGERATES

() {RETURN

Getxmldoc

();

WebMethod

]

Public DataSet GetForeignExchangeRatesDataSdataset

() {RETURN

Getxmldoc

().

DataSet

} [

WebMethod

]

Public String getBankPage

() {RETURN

GetWebContent

(

"http://www.bank-of-khina.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-khina.com/info/whjrpj.html"

);

// prepair for dataset

DataSet DS = New

DataSet

(

Exchange

);

DataTable DT

= New

Datarable

(

"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

(

"Banksell"

,

Typeof

Double));

DT

.

Columns

.

Add

(

"Baseline"

,

Typeof

Double));

XMLDATADOCUMENT XMLDOC

= New

XMLDATADOCUMENT

(

DS

);

Regex EXPR

= New

Regex

(@

" (? . *) / s *"

@

"

(? / D *.? / d *) (& nbsp) .? / s *"

@

"

(? / d *.? / d *) (& nbsp) .? / s *"

@

(? / D *.? / D *) (& nbsp) .? / s * "

@

(? / d *.? / d *) (& nbsp) .? / 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

[

"Sell"

].

Tostring

();

Row

[

"Banksell"

] =

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 with WSDL, it will be called directly. Since I let the Server side return DataSet, the client directly uses DataGrid to display Dataset, very easy, on this issue There is no technical key point in the client.

Using system

;

Using system

.

Threading

;

Using system

.

Drawing

;

Using system

.

Collectes

;

Using system

.

ComponentModel

;

Using system

.

Windows

.

Forms

;

Namespace BankDataClient

{

///

/// summary description for frmmainbankrates. ///

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

;

///

/// Required designer variable. ///

Private system

.

ComponentModel

.

Container Components

=

NULL

;

Public frmmainbankrates

() {

//////// Required for Windows Form Designer Support ///

InitializeComponent

();

Try

{

TXTURL

.

TEXT

=

SYSTEM

.

CONFIGURATION

.

ConfigurationSettings

.

Appsettings

[

"URL"

];

Proxy

.

URL

=

TXTURL

.

TEXT

}

Catch

(

EXCEPTION

) {

Proxy

.

URL

=

"http://www.dancefires.com/chinabank/foreignexchange.asmx"

;

TXTURL

.

TEXT

=

Proxy

.

URL

}}

///

/// clean up any resources being used. ///

Protected Override Void Dispose

(

Bool Disposing

) {IF

Disposing

) {IF

Components

! =

NULL

) {

Components

.

Dispose

();

Base

.

Dispose

(

Disposing

}

#region Windows Form Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// .

Private vid initializecomponent

() {

THIS

.

DataGrid1

= New

SYSTEM

.

Windows

.

Forms

.

DataGrid

();

THIS

.

DS

= New

SYSTEM

.

Data

.

DataSet

();

THIS

.

BtnConnect

= New

SYSTEM

.

Windows

.

Forms

.

Button

();

THIS

.

TXTURL

= 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

.

DataGrid1

.

Location

= New

SYSTEM

.

Drawing

.

Point

(

32

,

48

);

THIS

.

DataGrid1

.

Name

=

"DataGrid1"

;

THIS

.

DataGrid1

.

Size

= New

SYSTEM

.

Drawing

.

Size

(

480

,

256

);

THIS

.

DataGrid1

.

TabINDEX

=

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

=

"connection"

;

THIS

.

BtnConnect

.

Click

= New

SYSTEM

.

EventHandler

(

THIS

.

BtnConnect_click

);

/// txturl /////

THIS

.

TXTURL

.

Location

= New

SYSTEM

.

Drawing

.

Point

(

32

,

16

);

THIS

.

TXTURL

.

Name

=

"txturl"

;

THIS

.

TXTURL

.

Size

= New

SYSTEM

.

Drawing

.

Size

(

384

,

20

);

THIS

.

TXTURL

.

TabINDEX

=

2

;

THIS

.

TXTURL

.

TEXT

=

""

;

///Frmmainbankrates //

THIS

.

AutoScaleBaseSize

= New

SYSTEM

.

Drawing

.

Size

(

5

,

13

);

THIS

.

Clientsize

= New

SYSTEM

.

Drawing

.

Size

(

544

,

318

);

THIS

.

CONTROLS

.

Add

(

THIS

.

TXTURL

);

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

.

ResMelayout

(

False

}

#ndregion

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

[]

argg

) {

Application

.

Run

(New

frmmainbankrates

());}}}

With this example, you should be able to learn about the most basic XML, WebService, Regular Expression, DataSet, DataGrid knowledge. Software all code, and related screenshots can be obtained from the following connections: http://www.dancefires.com/chinabank/

转载请注明原文地址:https://www.9cbs.com/read-110406.html

New Post(0)