javascript - 我可以根据单元格的值使用 Excel 解析 html 文件中某个标记的值吗?

标签 javascript html excel vba

如果问题不是很清楚,我们深表歉意。这是更彻底的版本:

我有一个包含两列的电子表格:文件路径和文章标题。文件路径包含文章(html 文件)的路径,我将其标题从 html 文件手动复制并粘贴到另一列中。我需要执行此操作数百次,所以我很好奇是否有一种方法可以使其自动化。文章标题可在第一个 <span> 中找到第二个<h2>在每个 html 页面上。

示例:

单元格 A1:F:\2003\030714.html

单元 B1:篮子编织艺术

单元格 A2:F:\2003\030718.html

细胞 B2:为猫做饭

是否有某种魔法可以帮助实现这一目标?如果我能做一个 VLOOKUP ,这本来是小菜一碟,但不幸的是,我的初级 Web 开发人员和中级 excel 用户都感到困惑。

提前致谢!

最佳答案

选择包含要更新文章标题的文件路径的单元格范围,然后运行此过程。它将检查每个文件是否存在,如果存在,它将创建一个文件流对象来打开和读取该文件。它将返回文章标题作为第一个 Span 标签之后第二组 H2 标签之间的文本。不允许检查是否已到达第一个 Span 标记的末尾。希望这会有所帮助。

Sub UpdateArticleTitle()

Dim rngPath As Range
Dim tsObj As Object, tsFile As Object
Dim strLine As String
Dim bytSpanCount As Byte, bytH2Count As Byte
Dim strArticleTitle As String

    ' Go throught the range of selected fileds
    For Each rngPath In ActiveWindow.RangeSelection
        ' Continue if the file exists
        If Dir(rngPath.Value, vbNormal) <> "" Then
            ' Initialize the variables
            bytSpanCount = 0
            bytH2Count = 0
            strArticleTitle = ""
            ' Create a file system object
            Set tsObj = CreateObject("Scripting.FileSystemObject")
            ' Open the HTML file
            Set tsFile = tsObj.Opentextfile(rngPath.Value)
            Do Until tsFile.AtEndOfStream
                ' Read the file
                strLine = tsFile.ReadLine
                ' Search for the first occurrence of <span>
                If bytSpanCount = 0 Then
                    If InStr(1, LCase(strLine), "<span>") > 0 Then bytSpanCount = 1
                ' If <span> has been found, then search for <h2>
                ElseIf bytSpanCount = 1 Then
                    If InStr(1, LCase(strLine), "<h2>") > 0 Then
                        If bytH2Count = 0 Then
                            bytH2Count = 1
                        ' The second occurence of <h2> has been reached so extract the Article Title
                        Else
                            ' Get all lines until the closing </h2> tag is found
                            Do Until InStr(1, LCase(strLine), "</h2>") > 0
                                strLine = strLine & tsFile.ReadLine
                            Loop
                            ' Set the article title
                            strArticleTitle = Mid(strLine, InStr(1, LCase(strLine), "<h2>") + Len("<h2>"), InStr(1, LCase(strLine), "</h2>") - InStr(1, LCase(strLine), "<h2>") - Len("<h2>"))
                            ' Exit the loop
                            Exit Do
                        End If
                    End If
                End If
            Loop
            ' Close the file
            tsFile.Close
            ' Update the article title in the sheet
            rngPath.Offset(0, 1).Value = strArticleTitle
        Else
            ' Clear the article title if the file isn't found
            rngPath.Offset(0, 1).ClearContents
        End If
    Next rngPath

    Set tsObj = Nothing
    Set tsFile = Nothing

End Sub

关于javascript - 我可以根据单元格的值使用 Excel 解析 html 文件中某个标记的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27278022/

相关文章:

javascript - 使用 servlet 响应中的 javascript 显示图像

javascript - 单击选择框时的 Ajax 弹出窗口

javascript - 为什么这个 JavaScript 不动态创建对象属性?

excel - 用不同的键求和值

java - 在selenium中读取excel文件时出错

javascript - 环境变量 ReferenceError Postman

html - 删除 div 周围的空间

javascript - Chrome 扩展程序不在文档启动时运行脚本且不打印内容

html - div 内溢出的表格

vba - 使用用户表单在一行中搜索和更新值