PIXNET Logo登入

電腦購物天堂

跳到主文

各種商品資訊

部落格全站分類:數位生活

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 5月 16 週一 201118:21
  • [VBA]取出網頁的特定資料


展開/關閉程式碼







Public pURL As String
Public FindLinks As Boolean
Public rRow As Integer


Sub FindURL(sURL As String)
    Dim IE As New InternetExplorer
    Dim oDoc As New MSHTML.HTMLDocument
    Dim oLink As HTMLAnchorElement
    Dim i As Integer
    FindLinks = False
    IE.navigate sURL
    Do While IE.ReadyState <> READYSTATE_COMPLETE
        UrlForm.Label1.Caption = "網頁:" & sURL & "   連線中請稍候......"
        DoEvents
    Loop
    UrlForm.Label1.Caption = ""
    '引用 Document 對象
    Set oDoc = IE.Document
    '列出網頁中的新聞資料
    Call ListTableinnertext(oDoc)
    '獲取以順序排列的HTML 標籤中所有 Links 對象的集合。
    For i = 0 To oDoc.Links.Length - 1
        On Error Resume Next
        '尋找 Links 對象中innerText為"下頁"的標籤
        '因為網站為簡體,而我是使用繁體,所以才會使用 Like "下*"的方法
        '如果是簡體的環境可以直接改成 oDoc.Links(i).innerText="下頁"
        If Len(oDoc.Links(i).innerText) = 2 And oDoc.Links(i).innerText Like "下*" Then
            'href:獲取目標URL(完整網址)
            UrlForm.ListBox1.AddItem oDoc.Links(i).href
            Set oLink = oDoc.Links(i)
            If Not oLink Is Nothing Then
                pURL = oLink.href
                UrlForm.WebBrowser1.navigate pURL
                FindLinks = True
            End If
        End If
    Next i
    If FindLinks = False Then pURL = ""
    Set oDoc = Nothing
    Set IE = Nothing
End Sub


Sub ListTableinnertext(oDoc)
    Dim DocElemsCnt As Integer
    Dim Tbl As Object
    For DocElemsCnt = 0 To oDoc.all.Length - 1
        'tagName:獲取對象的標籤名稱。
        If oDoc.all.Item(DocElemsCnt).tagName = "TABLE" Then
            Set Tbl = oDoc.all.Item(DocElemsCnt)
            '每個網頁有很多TABLE(表格),而要取得資料的TABLE(表格)共有40列, _
             可以利用此特性來取得正確的TABLE(表格)
            If Tbl.Rows.Length > 20 Then
            'Tbl.Rows.Length:取得TABLE(表格)的列數
                For RwLen = 0 To Tbl.Rows.Length - 1
                    rRow = rRow + 1
                    'Int(Tbl.Cells.Length / Tbl.Rows.Length):可取得TABLE(表格)共有幾欄
                    For Colen = 0 To Int(Tbl.Cells.Length / Tbl.Rows.Length) - 1
                        Cells(rRow, Colen + 1).Value = Tbl.Rows(RwLen).Cells(Colen).innerText
                    Next Colen
                Next RwLen
            End If
        End If
    Next DocElemsCnt
    Application.Goto Cells(rRow, 1), Scroll:=True
End Sub


Sub FormShow()
    UrlForm.Show 0
End Sub
 

 




(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(4,892)

  • 個人分類:VB
▲top
  • 5月 16 週一 201116:44
  • [VB]WebBrowser.ObjectForScripting




使用WebBrowser 控件,可以通過 ObjectForScripting 和Document 
屬性在客戶端應用程序代碼和網頁腳本代碼之間實現雙向通信


'宣告

Public Property ObjectForScripting As Object



'用途
Dim instance As WebBrowser
Dim value As Object
value = instance.ObjectForScripting
instance.ObjectForScripting = value


(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(588)

  • 個人分類:VB
▲top
  • 5月 16 週一 201116:42
  • [C#]JavaScript與C# Windows應用程序交互

一、建立網頁
<html>
<head>
       <meta http-equiv="Content-Language" content="zh-cn">
       <script language="javascript" type="text/javascript">
              
<!-- 提供給C#程序調用的方法 -->
              
function messageBox(message)
              
{
                    alert(message);
             }

       
</script>
</head>
<body>
       <!-- 調用C#方法 -->
       <button onclick="window.external.MyMessageBox('javascript訪問C#代碼')" >
       javascript訪問C#代碼</button>
</body>
</html>
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(1,138)

  • 個人分類:VB
▲top
  • 5月 16 週一 201116:15
  • [VB]webbrowser 控件實現WinForm與WebForm交互



WebBrowser 控件可以讓你裝載Windows Form 應用程序中的 Web 網頁和其它採用瀏覽器的文件。可以使用webbrowser 控件將現有的web框架控制項加入至 Windows Form 客戶端應用程序。



還是直接看代碼吧。


WebBrowser 控制項 提供的屬性、方法和事件,可用來實現 Internet Explorer 的控制項
webBrowser1.Navigate(
"www.cnblogs.com"); //將指定位置處的文件載入至 WebBrowser
webBrowser1.GoBack();//上一頁
webBrowser1.GoForward();//下一頁
webBrowser1.Refresh();//刷新
webBrowser1.GoHome();//主頁
這裡提供了WebBrowser常用的方法,



上面的代碼是將 我們園子的主頁載入到WebBrowser控件中。如果我們想要在應用程式中產生自己的網頁內容,可以設定DocumentText屬性。也可以通過Document屬性來處理目前的網頁內容。如下代碼是使用DocumentText 屬性,顯示網頁內容。並用Document屬性來處理所顯示的網頁。

 



private void btnDocumentText_Click(object sender, EventArgs e)
{
string szHtml = @"
<HTML>
<HEAD>
<TITLE> DocumentText </TITLE>
</HEAD>
<BODY>
Please enter your name:<br/>
<input type='text' name='Name'/><br/>
<a href='http://www.microsoft.com' >Send input to method of Form class</a>
</BODY>
</HTML>
";
webBrowser1.DocumentText
= szHtml;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document
= this.webBrowser1.Document;
if (document != null && document.All["Name"] != null && String.IsNullOrEmpty(document.All["Name"].GetAttribute("value")))
{
e.Cancel
= true;
System.Windows.Forms.MessageBox.Show(
"You must enter your name before you can navigate to " + e.Url.ToString());
}
}



既然我們可以通過DocumentText生成自己的網頁,那麼能不能像使用IE那樣操作這個網頁呢?,答案是肯定的,完全可以像操作Web程序那樣操作WebBrowser 控制項。比如我們可以加入腳本,CSS。當然,如果你熟悉 HTML 物件對像模型 (DOM),也可以透過 Document 屬性來處理目前的Web網頁內容。下面的例子加入了JavaScript腳本來控制網頁。如果要在Winfrom程序中寫大量的Javascriot代碼,而且這些代碼最終要轉換成String型載入到Webbrowser 那將是很痛苦的事情,不過沒有關係,我們可以創建一個js文件,放入資源中,用的時候只需從資源中載入即可。這裡我創建一個名為 ClientScript.js 的文件



<script language = "javascript">
function ClickEvent(name)
{
alert(
"Hello: " +name);
}
function KeyDown()
{
if (event.keyCode==116)
{
event.keyCode=0;
event.returnValue=false;
}
return false;
}







string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
szClientScript
+
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onclick = 'ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText
= szWebBrowserText;




WebBrowser 是 System.Windows.Forms 下的控制項,也就是意味著它是用在WimForm程序下,那麼WebWrower所載入的Web頁面如何實現在WinForm程序下處理呢。例如上例中的 "Click Here" 。這裡的Click事件是通過腳本處理的,如何讓這個Click事件在Winform中處理呢?這裡要做一些修改。若要從指令碼存取用戶端應用程式,需要設定ObjectForScripting屬性。指令碼可以將您指定的物件當做window.external 物件來存取。
使用ObjectForScripting屬性,可啟用 WebBrowser 控制項所裝載之 Web 網頁與包含 WebBrowser 控制項之應用程式間的通訊。
這個屬性可讓您整合動態超文字標記語言 (DHTML) 程式碼與用戶端應用程式程式碼。
指定給這個屬性的物件可讓 Web 網頁指令碼做為 window.external 物件,這個物件是為了存取主應用程式而提供的內建 DOM 物件。



private void btnScriptEvent_Click(object sender, EventArgs e)
{
// This is the handler for loading the script into the Web Browser control and allowing us to interact
// between the script in the Browser control and this form class
// Set the ObjectForScripting property of the Web Browser control to point to this form class
// This will allow us to interact with methods in this form class via the window.external property
webBrowser1.ObjectForScripting = this;
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onClick='window.external.ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText
= szWebBrowserText;
}
public void ClickEvent(string userName)
{
// Simply echo out the name that the user typed in the input box of the HTML page
if (System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft == true)
MessageBox.Show(
"Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
else
MessageBox.Show(
"Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}




這裡的ObjectForScripting 屬性設置為 this。注意:在From1 類的開頭加入了這麼一句[ComVisible(true)], 它在System.Runtime.InteropServices下,預設值為 true,指出 Managed 型別對於 COM 為可見的。
 [ComVisible(true)]
 public partial class Form1 : System.Windows.Forms.Form
結束語:
本文簡單的介紹了 WebBrowser 的用法,實現了WinForm程序下與Web頁面的交互使用。本人在院子裡一直以來都是看客,這是第一次在園子裡留下技術足跡。有不妥之處,還望各位朋友指正。
感謝您能看到這裡。



綠色通道:好文要頂關注我收藏該文與我聯繫











 



 







(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(3,791)

  • 個人分類:VB
▲top
  • 5月 16 週一 201116:11
  • [VB] WebBrowser 判斷該網頁己被下載完成

問:
請問一下 我用 WebBrowser 的控制項設計一Browser,我想知道如何判斷該網頁己被瀏覽器下載完成?
答:
當網頁下載完成時,便會觸發webbrowser1_navigatecomplete2的事件!!!
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(390)

  • 個人分類:VB
▲top
  • 4月 29 週五 201117:42
  • [VB][系統]如何在另一個form顯示圖片

在form1內有一個PictureBox1工具上巳顯示圖片
加了一個Button1工具去連結到一個新的form2裡PictureBox2做圖片放大功能
其中要在另一個form顯示圖片要如何實現?
 
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(587)

  • 個人分類:VB
▲top
  • 4月 29 週五 201117:39
  • [VB][系統]如何製作延遲時間

1.System.Threading.Thread.CurrentThread.Sleep(1000)
2.Sub delay()
     Dim Start As Integer= Environment.TickCount()
     Dim
TimeLast As Integer = 7000 ' 要延遲 t 秒,就設為 t *1000
     Do
     If
Environment.TickCount() - Start> TimeLast Then Exit Do
    
Application.DoEvents() ' 要記得寫這行,不然都在跑迴圈,畫面可能會不見
     Loop
   End
Sub
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(4,275)

  • 個人分類:VB
▲top
  • 4月 29 週五 201117:35
  • [VB][網頁]webBrowser如何套用代理伺服器

Imports
System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential)>
_
    Public Structure Struct_INTERNET_PROXY_INFO
     Public dwAccessType
As Integer
     Public proxy As IntPtr
     Public proxyBypass As
IntPtr
    End Structure
    <DllImport("wininet.dll",
SetLastError:=True)> _
    Private Shared Function InternetSetOption(
_
     ByVal hInternet As IntPtr, _
     ByVal dwOption As Integer,
_
     ByVal lpBuffer As IntPtr, _
     ByVal lpdwBufferLength As Integer)
_
     As Boolean
    End Function
    Private Function
RefreshIESettings(ByVal strProxy As String) As Boolean
     Dim
INTERNET_OPTION_PROXY As Integer = 38
     Dim INTERNET_OPEN_TYPE_PROXY As
Integer = 3
     Dim struct_IPI As Struct_INTERNET_PROXY_INFO
    
'Filling in structure
     struct_IPI.dwAccessType =
INTERNET_OPEN_TYPE_PROXY
     struct_IPI.proxy =
Marshal.StringToHGlobalAnsi(strProxy)
     struct_IPI.proxyBypass =
Marshal.StringToHGlobalAnsi("local")
     'Allocating memory
     Dim
intptrStruct As IntPtr =
Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI))
     'Converting
structure to IntPtr
     Marshal.StructureToPtr(struct_IPI, intptrStruct,
True)
     Return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY,
intptrStruct, Marshal.SizeOf(struct_IPI))
    End Function
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(219)

  • 個人分類:VB
▲top
  • 4月 29 週五 201116:42
  • [VB][IO]如何存取檔案

在VB的檔案存取方式裡,最基本的觀念就是「開檔」→「處理檔」→「關檔」。
如果要將資料存入檔案裡,操作順序為「開檔」→「寫檔」→「關檔」,如此能將資料寫入檔案裡。
如果要將資料從檔案讀出,操作順序為「開檔」→「讀檔」→「關檔」,如此取得檔案裡的資料。
存入檔案
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(4,441)

  • 個人分類:VB
▲top
  • 4月 29 週五 201116:40
  • [VB][網頁]TextBox中輸完資料按enter的事件

Private Sub TextBox1_KeyDown(ByVal
sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles
TextBox1.KeyDown
     If e.KeyCode = Keys.Return Then
    
MessageBox.Show(TextBox1.Text)
     End If
End Sub
(繼續閱讀...)
文章標籤

pcman 發表在 痞客邦 留言(0) 人氣(1,812)

  • 個人分類:VB
▲top
123»

GTV 大視界

文章分類

toggle OS (2)
  • LINUX (13)
  • Andriod (1)
toggle 案件 (1)
  • 嵌入式系統 (0)
toggle 電子電路 (1)
  • 顯示器 (1)
toggle 程式語言 (1)
  • VB (24)
toggle 流行女裝 (2)
  • 白鳥花子 (0)
  • 天母嚴選 (0)
  • 硬碟 (1)
  • 處理器 (2)
  • 顯示卡 (5)
  • 喇叭音效 (1)
  • 電源 (1)
  • 螢幕 (5)
  • 未分類文章 (1)

文章搜尋

熱門文章

  • (4,441)[VB][IO]如何存取檔案
  • (4,275)[VB][系統]如何製作延遲時間
  • (2,903)動態對比是什麼
  • (1,697)GUID磁碟分割表
  • (1,549)[VB][網頁]vb.net介面程式自動連線至網頁,並且自動發訊息至手機上
  • (1,017)[VB][網頁]如何抓到網頁中圖片的URL
  • (721)[VB][網路]VB.NET 如何讀取網頁.並自動跳下一頁
  • (665)System Call 與驅動程式的關係
  • (437)[linux]Device File
  • (364)[VB][網路]WebBrowser載入去的網頁如果有捲軸時,自動移到最下頁

最新文章

  • [VBA]取出網頁的特定資料
  • [VB]WebBrowser.ObjectForScripting
  • [C#]JavaScript與C# Windows應用程序交互
  • [VB]webbrowser 控件實現WinForm與WebForm交互
  • [VB] WebBrowser 判斷該網頁己被下載完成
  • [linux]Kernel使用的資料結構
  • [linux]Block Device Driver和 Character Device Driver
  • [linux]QEMU
  • [linux]將 driver 掛載到 kernel 上
  • [linux]撰寫 driver 程式

BloggerADS

最新留言

    文章精選

    VB教學網站

    誰來我家

    參觀人氣

    • 本日人氣:
    • 累積人氣:

    站長工具