C# 从动态网站获取数据
我们有一个旧的windows窗体应用程序,其代码类似于C# 从动态网站获取数据,c#,asp.net,angularjs,parsing,web-crawler,C#,Asp.net,Angularjs,Parsing,Web Crawler,我们有一个旧的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; StreamR
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>
09:05
如何使用动态字段更新获取数据?如果不运行Javascript处理视图,使用Angular或其他Javascript框架构建的网页将基本上无法使用。你最好的选择是运行一个无头浏览器,并删除生成的HTML
根据服务器的不同,您可以请求预渲染版本。一些有角度的网站这样做是为了搜索引擎的目的,因为搜索引擎爬虫也不运行Javascript,并且面临与现在相同的问题。您必须检查所查询的任何服务,以确定这是否是一个选项。您始终可以打开网络查看器,查看它是否从特定端点提取数据,您可能能够将客户端设置为到达端点。。后台可能是json流或xml流。不久前,我不得不这样做,并找到了一个隐藏的api等价物,我可以询问