Page 1 of 1

ทำ Scraping WebSite แต่ VBA หา href ใน Tagname ไม่เจอครับ

Posted: Sat Oct 05, 2019 2:41 am
by SunSolar
ผมจะทำ Scraping WebSite แต่ VBA หา href ใน Tagname "a" ไม่เจอครับ

โค้ด VBA ดังนี้ครับ
-----------------------------------
Sub Getdata01()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As Object
Dim butt As Object

'Creat a new instance of ie
Set ieApp = New InternetExplorer

'Show Internet Explorer
ieApp.Visible = True

'Enter Website
ieApp.Navigate "http://welfare.dla.go.th/"

'wait for page to finish loading
Do While ieApp.Busy
DoEvents
Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop

Set ieDoc = ieApp.Document.getElementById("Login")
ieDoc("Login_username").Value = "xxxxxxx"
ieDoc("Login_password").Value = "xxxxxxx"
ieDoc.Document.getElementsByName("method:login")(0).Click

'wait for page to finish loading
Do While ieApp.Busy
DoEvents
Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop

''Open Search Link in Left Tab
Dim HTMLDOC As MSHTML.HTMLDocument
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
Dim HTMLDIV As MSHTML.IHTMLElement

Set HTMLDOC = ieApp.Document
Set HTMLAs = HTMLDOC.getElementsByTagName("a")

For Each HTMLA In HTMLAs

Debug.Print HTMLA.getAttribute("href") <<<<<<พอรันแล้วตรงนี้ไม่แสดงค่าอะไรเลยครับ เหมือนจะหา href ไม่เจอครับ

If HTMLA.getAttribute("href") = "http://welfare.dla.go.th/pages/Applican ... rch.action" Then
HTMLA.Click
Exit For
End If
Next HTMLA

Debug.Print HTMLAs.Length

End Sub
-------------------------------------------

(ตรง Set HTMLAs = HTMLDOC.getElementsByTagName ผมลองเปลี่ยนชื่อ Tagname จาก ("a") เป็น ("Link") แทน ปรากฎว่าหา href เจอ 2 อันครับ แต่พอเป็น ("a") กลับหา href ไม่พบครับ)

ไม่ทราบว่าผิดตรงไหน รบกวนช่วยแนะนำด้วยครับ ขอบคุณครับ


(สำหรับโค้ดของ HTML ตามเอกสารแนบครับ)

Re: ทำ Scraping WebSite แต่ VBA หา href ใน Tagname ไม่เจอครับ

Posted: Sat Oct 05, 2019 8:29 am
by snasui
:D กรุณาแนบไฟล์ที่เขียน VBA เอาไว้แล้วมาด้วยจะได้สะดวกในการทดสอบครับ

สำหรับการโพสต์ Code กรุณาอ่านกฎการใช้บอร์ดข้อ 5 ด้านบนประกอบ อักษรที่เป็น Code ควรจะแสดงในรูปแบบ Code จะได้สะดวกในการอ่านเพราะมีลักษณะแตกต่างจากข้อความปกติอย่างชัดเจน นอกจากนั้นยังสะดวกต่อการ Copy ไปทดสอบครับ