1

我正在使用 WebHarvest 从需要登录的站点获取数据。

它的设置是这样的:

第 1 页 = 登录页面

第 2 页 = 登录验证页面

第 3 页 = 统计页面

在第 2 页设置了 cookie。使用 Firebug 监视第 2 页的打开时,我得到以下标题:

Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8
Date    Tue, 23 Oct 2012 18:25:12 GMT
Keep-Alive  timeout=15, max=100
Server  Apache/2.0.64 (Win32) JRun/4.0 SVN/1.3.2 DAV/2
Set-Cookie  SESSION=hej123;expires=Thu, 16-Oct-2042 18:25:12 GMT;path=/
Transfer-Encoding   chunked

使用 WebHarvest 调用同一页面时,我只得到这些标题:

Date=Tue, 23 Oct 2012 18:31:51 GMT
Server=Apache/2.0.64 (Win32) JRun/4.0 SVN/1.3.2 DAV/2
Transfer-Encoding=chunked
Content-Type=text/html; charset=UTF-8

WebHarvest 似乎找不到三个标头(Set-Cookie、Connection 和 Keep-Alive)。第 1、2 和 3 页是虚拟的,因此没有进行实际验证。cookie 始终在服务器端为第 2 页设置。

这是我目前使用的 WebHarvest 代码:

<var-def name="content2">
<html-to-xml>
<http method="post" url="http://myurl.com/page2.cfm">
    <http-param name="Login">sigge</http-param>
    <http-param name="Password">hej123</http-param>
    <http-param name="doLogin">Logga in</http-param>
    <loop item="currField">
        <list>
            <var name="ctxtNewInputs" />
        </list>
        <body>
             <script><![CDATA[
                item = (NvPair) currField.getWrappedObject();
                SetContextVar("itemName", item.name);
                SetContextVar("itemValue", item.value);
            ]]></script>
            <http-param name="${item.name}"><var name="itemValue" /></http-param>
        </body>
    </loop>
     <script><![CDATA[
        String keys="";
        for(int i=0;i<http.headers.length;i++) {
            keys+=(http.headers[i].key + "=" + http.headers[i].value +"\n---\n");
        }
        SetContextVar("myCookie", keys);
    ]]></script>
    <file action="write" path="c:/kaka.txt">
        <var name="myCookie"/>
    </file>        
</http>
</html-to-xml>
</var-def>

编辑:检查时我注意到 cookie 是在 WebHarvest 中设置的,即使无法以编程方式找到 http 标头。是否有可能隐藏了某些响应标头的使用?

有谁知道这个问题的解决方法?

谢谢你和最好的问候, SiggeLund

4

1 回答 1

1

将 http 标头值获取到整个配置范围内的用户定义变量的方法如下:

<http url="your.url.here" method="GET">
    <!--Any settings you apply for the POST/GET call-->
</http>
<!--Now you've got your http object you are going to get header value from -->
<!--At it simplest the acquisition of value goes like the below-->
<var-def name="fifth_header_val">
      <script return="http.headers[5].value"/>
</var-def>

以上只是提供一个线索。您可以遍历 http.headers 索引并收集特定任务所需的键和值。

于 2015-05-05T16:38:45.137 回答