首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebScraping仅限于VBA中网页的特定部分

WebScraping仅限于VBA中网页的特定部分
EN

Stack Overflow用户
提问于 2021-12-05 22:35:48
回答 2查看 116关注 0票数 0

我正在重新浏览网页抓取,试图开发一个工具,可以从数据库中提取数据。

在这里,我使用了一个物质档案,发现在:https://echa.europa.eu/registration-dossier/-/registered-dossier/16016/7/1

下面列出了在本档案中可以找到的各种毒理学信息,但我只对DNELs的起始点(POD)感兴趣:

基本上,复制一个答案给我提供了一段时间前,我有以下的代码来提取第一个POD。

代码语言:javascript
复制
Public Sub GetContents()
    

'Start ECHA Search via XML HTTP Request

Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument

XMLReq.Open "Get", "https://echa.europa.eu/registration-dossier/-/registered-dossier/16016/7/1", False
XMLReq.send
 
If XMLReq.Status <> 200 Then
        
    MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
    Exit Sub

    End If
 
HTMLDoc.body.innerHTML = XMLReq.responseText


'Retrieve Data

'POD Population and Route
Set Info = HTMLDoc.getElementById("sWorkersHazardViaInhalationRoute")

Debug.Print Info.innerText

'POD Type
Set Info = HTMLDoc.getElementsByClassName("HorDL")(0)
Set data = Info.getElementsByTagName("dd")(0)
Debug.Print data.innerText

'POD Value
Set data = Info.getElementsByTagName("dd")(1)
Debug.Print data.innerText

End Sub

这可以为第一条管理路线: WorkersHazardViaDermalRoute提取过氧化物酶。

代码语言:javascript
复制
Workers - Hazard via inhalation route
DNEL (Derived No Effect Level)
238 mg/m³

这是很好的,但我真的希望能够调整这一点,以拉出DNEL及其价值的每一个管理路线。如蓝色突出显示:

因此,对于本例,整个所需的输出将跨3列(不重要,现在在3列中,尽管只是想要提取数据):

代码语言:javascript
复制
Workers - Hazard via inhalation route, DNEL (Derived No Effect Level), 238 mg/m³
Workers - Hazard via dermal route, DNEL (Derived No Effect Level), 84 mg/kg bw/day
General Population - Hazard via inhalation route, DNEL (Derived No Effect Level), 70 mg/m³
General Population - Hazard via dermal route, DNEL (Derived No Effect Level), 51 mg/kg bw/day
General Population - Hazard via oral route, DNEL (Derived No Effect Level), 24 mg/kg bw/day

我遇到的问题是,我使用类元素"HorDL“来获取这个信息,但不幸的是,这个类并不局限于每条路由以蓝色突出显示的部分。所以("HorDL")(0)可以找到,但是("HorDL")(1)会将相同路由的信息紧跟在下面。

出于这个原因,我怀疑使用这个类元素来提取信息并不是最好的方法,但是我想不出有什么其他方法可以做到这一点。

我已经有一种方法来提取相关的档案,所以,如果这是可行的,它将成为一个整洁的工具,刚刚提取了相关的信息。我已经考虑过提取所有信息,然后在excel中应用过滤器,但我不认为这是一个特别优雅的解决方案。

非常感谢您的回复。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-06 06:58:40

这假设您只需要标题中包含关键字WorkersGeneral Population的DNEL,其中包含Hazard for the eyes的DNEL。

注意:您应该声明您的所有变量,在模块顶部插入Option Explicit以帮助您执行它。

代码语言:javascript
复制
Option Explicit

Public Sub GetContents()
    Const DNELTitle As Long = 1
    Const DNELAssessment As Long = 2
    Const DNELValue As Long = 3
    
    Const resultFirstCell As String = "A1" 'Change the first cell address to insert the result accordingly
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change worksheet name accordingly
    
    'Start ECHA Search via XML HTTP Request
    Dim XMLReq As New MSXML2.XMLHTTP60
    Dim HTMLDoc As New MSHTML.HTMLDocument
    
    XMLReq.Open "Get", "https://echa.europa.eu/registration-dossier/-/registered-dossier/16016/7/1", False
    XMLReq.send
     
    If XMLReq.Status = 200 Then
        HTMLDoc.body.innerHTML = XMLReq.responseText
        
        '==== Loop through each anchors and get the relevant ID for interested DNEL
        Dim anchors As Object
        Set anchors = HTMLDoc.getElementById("SectionAnchors")
        Set anchors = anchors.getElementsByTagName("a")
        
        Dim anchorsColl As Collection
        Set anchorsColl = New Collection
        
        Dim i As Long
        For i = 0 To anchors.Length - 1
            Dim anchorText As String
            anchorText = anchors(i).innerText
            
            If InStr(anchorText, "Workers - ") <> 0 Or _
                InStr(anchorText, "General Population - ") <> 0 Then
                
                If InStr(anchorText, "Additional Information") = 0 And _
                    InStr(anchorText, "Hazard for the eyes") = 0 Then
                    
                    anchorsColl.Add Replace(anchors(i).href, "about:blank#", vbNullString)
                End If
            End If
        Next i
        '====
        
        If anchorsColl.Count <> 0 Then
            Dim outputArr() As String
            ReDim outputArr(1 To anchorsColl.Count, 1 To 3) As String
            
            For i = 1 To anchorsColl.Count
                Dim anchorEle As Object
                                
                Set anchorEle = HTMLDoc.getElementById(anchorsColl(i))
                outputArr(i, DNELTitle) = anchorEle.innerText
                
                'Loop through the anchor's sibling until it finds the DL tag to extract the values
                Do While anchorEle.nodeName <> "DL"
                    Set anchorEle = anchorEle.NextSibling
                Loop
                
                'Assumes that the assessment conclusion is in the first DD tag
                'Assumes that the value is in the second DD tag
                outputArr(i, DNELAssessment) = anchorEle.getElementsByTagName("dd")(0).innerText
                outputArr(i, DNELValue) = anchorEle.getElementsByTagName("dd")(1).innerText
            Next i
            
            'Write the extraction result to the worksheet starting from A1
            ws.Range(resultFirstCell).Resize(UBound(outputArr, 1), 3).Value = outputArr
        Else
            Debug.Print "No DNEL found."
        End If
        
        Set ws = Nothing
        Set HTMLDoc = Nothing
    Else
        MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
    End If
    
    Set XMLReq = Nothing
End Sub
票数 2
EN

Stack Overflow用户

发布于 2021-12-06 13:07:05

到目前为止我自己的答案。

接下来,我将让这个循环通过一个值列表来返回每个值的DNEL。还需要包括某种错误处理。

代码语言:javascript
复制
Sub GetData()
    

'Start ECHA Search via XML HTTP Request

Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument

XMLReq.Open "Get", "https://echa.europa.eu/registration-dossier/-/registered-dossier/16016/7/1", False
XMLReq.send
 
If XMLReq.Status <> 200 Then
        
    MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
    Exit Sub

    End If
 
HTMLDoc.body.innerHTML = XMLReq.responseText


'Retrieve Data for General population

'Defines class element for each route
Dim Route(1 To 3) As String

Route(1) = "sGeneralPopulationHazardViaInhalationRoute"
Route(2) = "sGeneralPopulationHazardViaDermalRoute"
Route(3) = "sGeneralPopulationHazardViaOralRoute"

'Loops through each element

r = 4
c = 6

Dim i As Long

For i = 1 To UBound(Route, 1)


Set Info = HTMLDoc.getElementById(Route(i))
Debug.Print Info.innerText

Set Info = HTMLDoc.getElementById(Route(i)).NextSibling.NextSibling.NextSibling
Set Data = Info.getElementsByTagName("dd")(0)
Debug.Print Data.innerText

Set Data = Info.getElementsByTagName("dd")(1)
Debug.Print Data.innerText


Cells(r, c) = Data.innerText

c = c + 1

Next i

r = r + 1


End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70239160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档