Share via


HOW TO:建立剖析網頁內容的 Web 服務

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

使用 ASP.NET 建立的 Web 服務會提供 HTML 剖析方案,讓開發人員用來剖析遠端 HTML 網頁的內容,並以程式設計方式公開產生的資料。如需詳細說明,請參閱 ASP.NET XML Web Service 進行的 HTML 剖析

若要指定作業和輸入參數

  1. 建立 Web 服務描述語言 (WSDL) 文件,此文件通常是以副檔名 .wsdl 儲存。文件的內容必須根據 WSDL 結構描述包含有效的 XML。如需原型,則可以使用針對在 ASP.NET 上執行的 Web 服務動態產生的 WSDL 文件。請將 ?wsdl 引數附加至 Web 服務 URL 以進行要求。

  2. 指定一些項目,用來定義每個剖析 HTML 文字之 Web 服務方法的作業。這個步驟和下一個步驟都需要有關 WSDL 格式的知識。

  3. 如果剖析方法接受輸入參數,請指定項目來表示這些參數,以及將這些參數作業與產生關聯。

若要指定從剖析的 HTML 網頁傳回的資料

  1. 在經由 XPath /definitions/binding/operation/output 出現的 <output> 項目內新增限定命名空間的 <text> XML 項目。<operation> 項目代表擷取剖析的 HTML 的 Web 服務方法。
xxb0bsdh.note(zh-tw,VS.100).gif注意:
為了避免與匯入相同應用程式中的其他 WSDL 檔案發生命名衝突,繫結內的作業名稱必須是全域而唯一的,或者也可以指定命名空間來執行 Wsdl.exe。

  1. 針對您要從剖析的 HTML 網頁傳回的每一項資料,在服務描述中的 <text> XML 項目內新增 <match> XML 項目。

  2. 將屬性套用至 <match> 項目。<ASP.NET XML Web Service 進行的 HTML 剖析>主題中有一份表格會列出有效的屬性。

若要產生 Web 服務的用戶端 Proxy 程式碼

  1. 從 Windows® 軟體開發套件 (SDK) 執行 Wsdl.exe 工具。傳遞您建立的 WSDL 檔案做為輸入。

範例

下列程式碼範例是簡單的網頁範例,其中包含 <TITLE><H1> 標記。

<HTML>
 <HEAD>
  <TITLE>Sample Title</TITLE>
 </HEAD>
 <BODY>
    <H1>Some Heading Text</H1>
 </BODY>
</HTML>

下列程式碼範例是一個服務描述,它會剖析 HTML 網頁的內容,並擷取 <TITLE><H1> 標記中的文字內容。在程式碼範例中,會針對 GetTitleHttpGet 繫結定義 TestHeaders 方法。TestHeaders 方法會定義兩項可在 <match> XML 項目中,從剖析的 HTML 網頁傳回的資料:TitleH1 (分別剖析 <TITLE><H1> 標記的內容)。

<?xml version="1.0"?>
<definitions xmlns:s="http://www.w3.org/2001/XMLSchema"
             xmlns:http="https://schemas.xmlsoap.org/wsdl/http/"
             xmlns:mime="https://schemas.xmlsoap.org/wsdl/mime/"
             xmlns:soapenc="https://schemas.xmlsoap.org/soap/encoding/"
             xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:s0="http://tempuri.org/"
             targetNamespace="http://tempuri.org/"
             xmlns="https://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema targetNamespace="http://tempuri.org/"
              attributeFormDefault="qualified"
              elementFormDefault="qualified">
      <s:element name="TestHeaders">
        <s:complexType derivedBy="restriction"/>
      </s:element>
      <s:element name="TestHeadersResult">
        <s:complexType derivedBy="restriction">
          <s:all>
            <s:element name="result" type="s:string" nullable="true"/>
          </s:all>
        </s:complexType>
      </s:element>
      <s:element name="string" type="s:string" nullable="true"/>
    </s:schema>
  </types>
  <message name="TestHeadersHttpGetIn"/>
  <message name="TestHeadersHttpGetOut">
    <part name="Body" element="s0:string"/>
  </message>
  <portType name="GetTitleHttpGet">
    <operation name="TestHeaders">
      <input message="s0:TestHeadersHttpGetIn"/>
      <output message="s0:TestHeadersHttpGetOut"/>
    </operation>
  </portType>
  <binding name="GetTitleHttpGet" type="s0:GetTitleHttpGet">
    <http:binding verb="GET"/>
    <operation name="TestHeaders">
      <http:operation location="MatchServer.html"/>
      <input>
        <http:urlEncoded/>
      </input>
      <output>
         <text xmlns="https://microsoft.com/wsdl/mime/textMatching/">
          <match name='Title' pattern='TITLE&gt;(.*?)&lt;'/>
          <match name='H1' pattern='H1&gt;(.*?)&lt;'/>
         </text>
      </output>
    </operation>
  </binding>
  <service name="GetTitle">
    <port name="GetTitleHttpGet" binding="s0:GetTitleHttpGet">
      <http:address location="https://localhost" />
    </port>
  </service>
</definitions>

下列程式碼範例是 Wsdl.exe 為上述服務描述所產生的 Proxy 類別部分。

' GetTitle is the name of the proxy class.
Public Class GetTitle
  Inherits HttpGetClientProtocol
  Public Function TestHeaders() As TestHeadersMatches
     Return CType(Me.Invoke("TestHeaders", (Me.Url + _
          "/MatchServer.html"), New Object(-1) {}),TestHeadersMatches)
  End Function
End Class
Public Class TestHeadersMatches    Public Title As String    Public H1 As String
End Class
' GetTitle is the name of the proxy class.
public class GetTitle : HttpGetClientProtocol
{
  public TestHeadersMatches TestHeaders() 
  {
        return ((TestHeadersMatches)(this.Invoke("TestHeaders", 
                 (this.Url + "/MatchServer.html"), new object[0])));
  }
}    
public class TestHeadersMatches 
{
    public string Title;    public string H1;
}

另請參閱

參考

Web 服務描述語言工具 (Wsdl.exe)
MatchAttribute

概念

ASP.NET XML Web Service 進行的 HTML 剖析

其他資源

.NET Framework Regular Expressions
使用 ASP.NET 的 XML Web Service