C#HTML Agility Pack从网站解析数据 [英] C# Html Agility Pack Parsing Data From Website
问题描述
我在解析网站数据时遇到问题. 当下载html并加载它时,html文档变为null.我也不能从表中解析任何数据,因为没有或在HTML文档中.行和列在表中有一部分,但它为空..
I have a problem with parsing data from a website. When downloaded html and loaded it html document turns null. Also i can't parse any data from table because no or in html document.Rows and columns are in part in table but its nulled..
有人帮助吗? 谢谢.. 这是我使用的代码;
Anyone help please ? Thanks.. This is code i used;
Uri uri =new Uri("https://deprem.afad.gov.tr/sondepremler.html");
HttpWebRequest webClient = (HttpWebRequest)WebRequest.Create(uri);
webClient.Method = "GET";
webClient.ContentType = "text/html;charset=utf-8";
HtmlDocument doc = new HtmlDocument();
using (var response = (HttpWebResponse)webClient.GetResponse())
{
using (var stream = response.GetResponseStream())
{
doc.Load(stream, Encoding.GetEncoding("utf-8"));
}
}
var tds = doc.DocumentNode.SelectNodes("//table//tr//td");
这是从网站转过来的html文档;
And this is the html document turned from website;
<table id="resultTable" class="table table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th id="thDate">Tarih(TS)</th>
<th>Ajans</th>
<th>Enlem</th>
<th>Boylam</th>
<th>Derinlik</th>
<!--<th>Rms</th> -->
<th>Tip</th>
<th>Büyüklük</th>
<th>Ülke</th>
<th>İl</th>
<th>İlçe</th>
<th>Köy</th>
<th>Diğer</th>
<th>EventID</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
推荐答案
访问站点时,可以按F12键并查看所有正在拨打的电话.您可以使用这些API调用自己通过Postman或通过使用Rest客户端的C#来检索数据.
When you are visiting a site, you can press F12 and see all the calls that are being made. You can use those API calls to retrieve the data yourself using Postman or via C# using Rest clients.
这是如何获取所需数据的示例.我在chrome上使用了开发工具,以查看在网络"标签下进行的通话.
This is an example of how you can get the data you are looking for. I used Dev tools on chrome to see the call being made under Network Tab.
public class Event
{
public string eventId { get; set; }
public string time { get; set; }
public string agency { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string depth { get; set; }
public string rms { get; set; }
public string type { get; set; }
public string m { get; set; }
public object place { get; set; }
public string country { get; set; }
public string city { get; set; }
public string district { get; set; }
public string town { get; set; }
public string other { get; set; }
public object mapImagePath { get; set; }
public object strike1 { get; set; }
public object dip1 { get; set; }
public object rake1 { get; set; }
public object strike2 { get; set; }
public object dip2 { get; set; }
public object rake2 { get; set; }
public object ftype { get; set; }
public object pic { get; set; }
public object file { get; set; }
public object focalId { get; set; }
public string time2 { get; set; }
}
您可以在主程序中使用上述类,
You can use the above class in main program like,
var client = new RestClient("https://deprem.afad.gov.tr/latestCatalogsList");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AlwaysMultipartFormData = true;
request.AddParameter("m", "0");
request.AddParameter("utc", "0");
request.AddParameter("lastDay", "1");
var response = client.Execute<List<Event>>(request);
List<Event> myData = response.Data;
Console.WriteLine(response.Content);
您将拥有一个包含该站点中所有数据的对象.您可以对这些数据进行任何所需的操作.
You will have an object with all the data from the site. You can do whatever you need to with that data.
请帮忙将帖子标记为已回答
这篇关于C#HTML Agility Pack从网站解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!