【问题标题】:Get data from dynamical websites从动态网站获取数据
【发布时间】:2016-02-19 20:25:57
【问题描述】:

我们有一个旧的 windows 窗体应用程序,使用类似于以下代码的代码

using System.Net;
using System.IO;
using System.Windows.Forms;

string result = null;
string url = "http://www.despegar.cl/shop/flights/results/oneway/ANF/SCL/2016-03-30/1/0/0?from=SB";
WebResponse response = null;
StreamReader reader = null;

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "GET";
    response = request.GetResponse();
    reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    result = reader.ReadToEnd();
}
catch (Exception ex)
{
    // handle error
    MessageBox.Show(ex.Message);
}
finally
{
    if (reader != null)
        reader.Close();
    if (response != null)
        response.Close();
}

从despegar cl等外部网站获取源代码,然后获取航班起飞时刻表的数据。 问题在于使用 AngularJS 或类似框架的页面在运行时替换了这些字段。获得的源代码类似于

<span class="hour">{{data.departure.hour.formatted}}</span>

当我们想要找到时

<span class="hour">09:05</span>

如何获取动态字段更新的数据?

【问题讨论】:

    标签: c# asp.net angularjs parsing web-crawler


    【解决方案1】:

    您可以随时打开网络查看器并查看它是否从特定端点提取数据,您也许可以将客户端设置为到达端点.. 可能是后台的 json 流或 xml 流.不久前我不得不这样做,并找到了一个隐藏的 api 等价物,我可以询问。

    【讨论】:

      【解决方案2】:

      如果不运行 Javascript 来处理视图,使用 Angular 或其他 Javascript 框架构建的网页将在很大程度上无法使用。最好的办法是运行无头浏览器并抓取生成的 HTML。

      根据服务器,您可能可以请求预渲染版本。一些 Angular 网站出于搜索引擎的目的这样做,因为搜索引擎爬虫也不运行 Javascript,并且面临与您现在相同的问题。您必须检查您要查询的任何服务,看看这是否是一个选项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-28
        • 2021-10-07
        • 1970-01-01
        相关资源
        最近更新 更多