首先:鼠標(biāo)API函數(shù)
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站,網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
[DllImport("User32")]
public extern static bool GetCursorPos(out POINT p);
public struct POINT
{
public int X;
public int Y;
}
public enum MouseEventFlags
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
Wheel = 0x0800,
Absolute = 0x8000
}
其次:載入webBrowser
private void FormBrower_Load(object sender, EventArgs e)
{
webBrowser.Navigate("");
}
接著:根據(jù)鼠標(biāo)API獲取到X,Y坐標(biāo)區(qū)域。。
如何通過鼠標(biāo)點(diǎn)擊,獲取的區(qū)域,導(dǎo)出網(wǎng)頁元素的XPATH?
解析步驟:
創(chuàng)建一個(gè)HtmlElment2的getBoundingClientRect方法得到元素矩形(Rect),即元素Webbrowser位置。再通過offsetTop, .offsetLeft, .offsetWidth, .offsetHeight四個(gè)屬性,獲取絕對位置。
我知道,WebBrowser.document.activeElement.ID能夠獲取到某元素的ID。
但是,,如何獲取到網(wǎng)頁元素的XPATH,始終搞不出來。。。
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim stream As IO.Stream = WebRequest.Create(UrlAdress).GetResponse().GetResponseStream()
'注意urladress為你上面的網(wǎng)頁地址。
Dim sr As StreamReader = New StreamReader(stream, System.Text.Encoding.UTF8)
Label1.Text = Regex.Match(sr.ReadToEnd, "回答采納率").ToString
'sr。readtoend讀取網(wǎng)頁流到末尾,即使用正則表達(dá)式從網(wǎng)頁流中提取“回答采納率”,賦值給Label1.Text ‘沒有則為空
sr.Dispose() '關(guān)閉流
End Sub'要提取什么東西用正則表達(dá)式最好
End Class
Public Function webCaptureContent(ByVal mWebsiteUrl As String, ByVal mWebsiteType As Boolean) As String
'啟動一次具體的數(shù)據(jù)采集工作,返回采集到的HTML內(nèi)容:要求必須輸入帶://的全地址數(shù)據(jù)
On Error Resume Next
Dim Str_WebContent As String = "請輸入查找網(wǎng)站地址."
Dim wb As WebClient = New WebClient() '//創(chuàng)建一個(gè)WebClient實(shí)例
If mWebsiteUrl.IndexOf("://") 0 Then
'//獲取或設(shè)置用于對向 Internet 資源的請求進(jìn)行身份驗(yàn)證的網(wǎng)絡(luò)憑據(jù)。(可有可無)
wb.Credentials = CredentialCache.DefaultCredentials
'//從資源下載數(shù)據(jù)并返回字節(jié)數(shù)組。(加@是因?yàn)榫W(wǎng)址中間有"/"符號)
Dim pagedata As Object = wb.DownloadData(mWebsiteUrl)
'//轉(zhuǎn)換字符
If mWebsiteType Then
Str_WebContent = Encoding.Default.GetString(pagedata)
Else
Str_WebContent = Encoding.UTF8.GetString(pagedata)
End If
End If
Return Str_WebContent '提取出來新聞內(nèi)容,刪除Body前后的多余內(nèi)容,同時(shí)補(bǔ)充上該 Body標(biāo)記,形成完整的內(nèi)容 Str_WebContent '
End Function