Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AppleScript解析HTML源代码_Html_Parsing_Applescript_Delimiter_Automator - Fatal编程技术网

使用AppleScript解析HTML源代码

使用AppleScript解析HTML源代码,html,parsing,applescript,delimiter,automator,Html,Parsing,Applescript,Delimiter,Automator,我正在尝试解析一个HTML文件,我已经在Automator内部将其转换为TXT文件 我以前使用Automator从一个网站下载了HTML文件,现在我正在努力解析源代码 最好,我只想获取表的信息,我需要对1800个不同的HTML文件重复这个操作 以下是源代码的示例: </head> <body> <div id="header"> <div class="wrapper"> <span class="access">

我正在尝试解析一个HTML文件,我已经在Automator内部将其转换为TXT文件

我以前使用Automator从一个网站下载了HTML文件,现在我正在努力解析源代码

最好,我只想获取表的信息,我需要对1800个不同的HTML文件重复这个操作

以下是源代码的示例:

</head>
<body>
<div id="header">
    <div class="wrapper">
        <span class="access">
        <div id="fb-root"></div>


    <span class="access">
     Gold Account: <a class="upgrade" title="Account Details" href="http://www.hedge-professionals.com/account-details.html" >Active </a>       Logged in as Edward&nbsp;&nbsp; | &nbsp;&nbsp;<a href="javascript:void(0);" onclick='logout()' class="logout">Sign Out</a>

    </span>
                                    </span>
    </div><!-- /wrapper -->
</div><!-- /header -->

<div id="masthead">
    <div class="wrapper">   
        <a href="http://www.hedge-professionals.com" ><img src="http://www.hedge-professionals.com/images/hedgep_logo_white.png" alt="Hedge Professionals Database" width="333" height="46" class="logo" border="0" /></a>
        <div id="navigation">
            <ul>
<li ><a href='http://www.hedge-professionals.com/dashboard.html' >Dashboard</a></li>    <li ><a href='http://www.hedge-professionals.com/people.html'class='current' >People</a></li><li ><a href='http://www.hedge-professionals.com/watchlists.html' >My Watchlists</a></li><li ><a href='http://www.hedge-professionals.com/my-searches.html' >My Searches</a></li><li ><a href='http://www.hedge-professionals.com/my-profile.html' >My Profile</a></li></ul>               
        </div><!-- /navigation -->

    </div><!-- /wrapper -->     
</div><!-- /masthead -->


<div id="content">
    <div class="wrapper">
        <div id="main-content">

 <!-- per Project stuff -->
    <span class="section">
                <img src="http://www.hedge-professionals.com/images/people/noimage_53x53.jpg" alt="Christian Sieling" width="52" height="53" class="profile-pic" id="profile-pic-104947"/>
                <h1><span id="profile-name-104947" >Christian Sieling</span></h1>
                                    <ul class="gbutton-group right">
                    <li><a class="gbutton bold pill" href="http://www.hedge-professionals.com/people.html">&laquo; Back </a></li>
                    <li><a class="gbutton bold pill boxy on-click" href="http://www.hedge-professionals.com/addtoWatchlist.php?usr=114752"  id="row-104947" title='Add to Watchlist' >Add to Watchlist</a></li>
                </ul>

                <div style="float:right;padding:3px 3px;text-align:center;margin-top:5px;" >
                <span id="profile-updated-date" >Updated On: 4 Aug, 2010</span><br/>
                <a class="gbutton bold pill" href="http://www.hedge-professionals.com/profile/suggest/people/104947/Christian-Sieling" style="margin:5px;" title='Report Inaccurate Data' >Report Inaccurate Data</a>
                </div>
                                    <h2><span id="profile-details-104947" > at <a href="http://www.hedge-professionals.com/quicksearch/search/Lumix+Capital+Management+Ltd." ><span title='Lumix Capital Management Ltd.' >Lumix Capital Management Ltd.</span></a></span><input type="hidden" name="sub-id" id="sub-id" value="114752"></h2>

            </span>

            <table width="100%" border="0" cellspacing="0" cellpadding="0" id="profile-table">
                                                        <tr>
                    <th>Role</th>
                    <td>
                    <p>Other</p>                            </td>
                </tr>
                <tr>  
                    <th>Organisation Type</th>
                    <td>
                    <p>Asset Manager</p>                        </td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td><a href="mailto:cs@lumixcapital.com" title="cs@lumixcapital.com" >cs@lumixcapital.com</a></td>
                </tr>
                <tr>
                    <th>Website</th>
                    <td><a href="http://www.lumixcapital.com/" target="_new" title="http://www.lumixcapital.com/" >http://www.lumixcapital.com/</a></td>
                </tr>
                <tr>
                    <th>Phone</th>
                    <td>41 78 616 7334</td>
                </tr>
                <tr>
                    <th>Fax</th>
                    <td></td> 
                </tr>
                <tr>
                    <th>Mailing Address</th>
                    <td>Birrenstrasse 30</td>
                </tr>
                <tr>
                    <th>City</th>
                    <td>Schindellegi</td>
                </tr>
                <tr>
                    <th>State</th>
                    <td>CH</td>
                </tr>
                <tr>
                    <th>Country</th>
                    <td>Switzerland</td>
                </tr>
                <tr>
                    <th class="lastrow" >Zip/ Postal Code</th>
                    <td class="lastrow" >8834</td>
                </tr>
        </table>
                </div><!-- /main-content -->
                    <div id="sidebar"  >
                    </div>

            <div id="similar_sidebar" class="similar_refine" >



            </div>
                            </div><!-- /wrapper -->
</div><!-- /content -->

<div id="footer">

</div>

如何解析HTML文件中的表?

你真的很接近了。问题在于startText变量。起始表标记不在html文本中,因此无法找到它。表的起始行实际上是

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="profile-table">
通过这种方式,我们可以忽略表标记附带的所有代码(宽度、边框等),因为我假设它在不同的文件之间会有所不同。完成此操作后,我们只得到表的代码。试试这个

set p to input
set ex to extractBetween(p, "<table", ">", "</table>")

to extractBetween(SearchText, startText1, startText2, endText)
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to startText1
    set endItems to text item -1 of SearchText
    set AppleScript's text item delimiters to endText
    set beginningToEnd to text item 1 of endItems
    set AppleScript's text item delimiters to startText2
    set finalText to (text items 2 thru -1 of beginningToEnd) as text
    set AppleScript's text item delimiters to tid
    return finalText
end extractBetween
将p设置为输入
将ex设置为介于(p,“,”之间)
提取(搜索文本、开始文本1、开始文本2、结束文本)
将tid设置为AppleScript的文本项分隔符
将AppleScript的文本项分隔符设置为startText1
将endItems设置为SearchText的文本项-1
将AppleScript的文本项分隔符设置为endText
将BeginingToEnd设置为endItems的文本项1
将AppleScript的文本项分隔符设置为startText2
将finalText设置为(BeginingToEnd的文本项2到-1)作为文本
将AppleScript的文本项分隔符设置为tid
返回finalText
端部拔出器
试试:

将xxx设置为别名“Mac OS X:Users:paolo:Desktop:paolo.html”
将yyy设置为执行shell脚本“echo”&引用xxx的形式&“grep-o\\”

您可以通过do javascript命令在Safari中利用HTML解析器,而不是创建自己的HTML解析器。JavaScript具有处理HTML元素和数据的内置功能

此脚本仅获取页面中第一个表的HTML:

tell application "Safari"
    tell document 1
        set theFirstTableHTML to do JavaScript "document.getElementsByTagName('table')[0].innerHTML"
    end tell
end tell

您可以使用此技术将基本DOM脚本应用于任何页面,并获取您想要读取的任何数据。您可以只获取表格单元格的值,或任何您想要的值。

一行代码:


告诉应用程序“Safari”将源代码设置为字符(偏移量
),但是,通过一个简单的“获取段落”,我能够选择我想要的内容
将源文件设置为(选择文件)//将newContent设置为读取源文件//获取newContent的第247至306段
这是一个单文件解决方案。不确定为什么它对您不起作用。它起作用了……相信我。您一定在做其他事情。正如adayzdone提到的,您的解决方案对多个文件不起作用。段落编号显然会改变每个文件都是ge的。@regulus6633您是对的,对不起。我试图修改HTML标记以更好地适应我正在分析的内容,但它不起作用(不确定原因,仍在尝试).但后来我用你提供的源代码试用了applescript,效果很好。谢谢,但如果applescript让你免于多年的繁重工作,你不会有这种感觉,就像它对出版业和其他创意领域的数百万人那样。
>
set p to input
set ex to extractBetween(p, "<table", ">", "</table>")

to extractBetween(SearchText, startText1, startText2, endText)
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to startText1
    set endItems to text item -1 of SearchText
    set AppleScript's text item delimiters to endText
    set beginningToEnd to text item 1 of endItems
    set AppleScript's text item delimiters to startText2
    set finalText to (text items 2 thru -1 of beginningToEnd) as text
    set AppleScript's text item delimiters to tid
    return finalText
end extractBetween
set xxx to read alias "Mac OS X:Users:paolo:Desktop:paolo.html"
set yyy to do shell script "echo " & quoted form of xxx & " | grep -o \\<table.*table\\>"
tell application "Safari"
    tell document 1
        set theFirstTableHTML to do JavaScript "document.getElementsByTagName('table')[0].innerHTML"
    end tell
end tell