如何讀取網頁表格欄位資料 |
答題得分者是:washare
|
BOSS
中階會員 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
編輯記錄
taishyang 重新編輯於 2007-05-21 10:00:40, 註解 將文章分類成[問題]‧
|
BOSS
中階會員 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
procedure TForm1.FormCreate(Sender: TObject);
begin WebBrowser1.Navigate('http://www.taiwanratings.com/tw/'); end; procedure TForm1.Button1Click(Sender: TObject); var doc:IHTMLDocument2; edits:OleVariant; begin doc:= WebBrowser1.Document as IHTMLDocument2; memo1.Clear ; memo1.Lines.add(doc.Body.OuterText) ; end; 各位前輩 我用上述的方法為何讀不到此網頁的原始碼 |
BOSS
中階會員 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
|
BOSS
中階會員 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
|
washare
初階會員 發表:14 回覆:36 積分:35 註冊:2006-10-17 發送簡訊給我 |
又幫你從網路上copy了一段程式碼,確定修改後是可用的,你自已參考看看吧
<textarea class="delphi" rows="10" cols="60" name="code"> Title: Parsing html document asked by Slavak on 09/21/2003 09:22AM PDT What is the best way to parse html document? For example, I download some html page from server and want read one of tables and show it on my string grid. Can I use WebBrowser.Document interface for this task? TWebbrowser can be used, try something like this: Start by including (importing from mshtml.dll) the MSHTML.pas file in uses. Cast the WebBrowser.Document as IHTMLDocument2 or IHTMLDocument3. var MyDocument: IHTMLDocument3; Tables: IHTMLElementsCollection; Table: IHTMLTable2; Row: IHTMLTableRow2; Cell: IHTMLTableCell; MyDocument := (webbrowser.Document as IHTMLDocument3); You can access all table elements within the document with: Tables := (MyDocument.Body.All.tags( 'TABLE' ) as IHTMLElementCollection); Then look for the element you like to find. This should be easy if it has a name, because you can use: Table := (Elements.item('NameOfMyTable',EmptyParam) as IHTMLTable2); Or you have to loop each one: for I := 0 to Tables.length - 1 do begin Table := Tables.item(EmptyParam,I) as IHTMLTable2; ... end; When you find the table, get hold of the Rows property and handle each row: for J:=0 to Table.Rows.Length-1 do begin Row := Table.Rows.Item(J,''); for K:=0 to Row.cells.Length-1 do begin Cell := Row( EmptyParam, K ) as IHTMLTableCell; StringGrid1.Cells[K,J] := (( Cell as IHTMLElement).innerText); end; end; Havent tried running this, but it should be about right... There are also plenty of different parsers on the net. Comment from Lummie Date: 09/22/2003 08:43AM PDT Comment If you want a parser that is not reliant on having MSHTML installed then I recommend Turbo Power Internet Professional it has a good parser (and browser component). It's on sourceforge at http://sourceforge.net/projects/tpipro/ Accepted Answer from EddieShipman Date: 09/30/2003 06:30AM PDT Grade: B Accepted Answer Here's how to do it with TWebBrowser: procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; ovTable: OleVariant; begin // I'm using the first TABLE on the page as an example only // You will have to determine which one you need to load from. ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0); for i := 0 to (ovTable.Rows.Length - 1) do begin for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do begin StringGrid1.Cells[i 1, j 1] := ovTable.Rows.Item(i).Cells.Item(j).InnerText; end; end; end; Comment from EddieShipman Date: 09/30/2003 06:30AM PDT Comment oops, this: StringGrid1.Cells[i 1, j 1] should be : StringGrid1.Cells[j 1, i 1] </textarea> |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |