C# Can';t使用HTML agility pack从HTML代码中获取a值
我想从HTML代码中获取一个值,我正在使用这个C#代码使用HtmlAgilityPack从这个HTML代码中获取值 我只想要地址和电话号码C# Can';t使用HTML agility pack从HTML代码中获取a值,c#,web-scraping,html-agility-pack,C#,Web Scraping,Html Agility Pack,我想从HTML代码中获取一个值,我正在使用这个C#代码使用HtmlAgilityPack从这个HTML代码中获取值 我只想要地址和电话号码 <div class="company-info"> <div id="o-company" class="edit-overlay-section" style="padding-top:5px; width: 400px;"> <a href="http://www.manta.com/c/mm23d
<div class="company-info">
<div id="o-company" class="edit-overlay-section" style="padding-top:5px; width: 400px;">
<a href="http://www.manta.com/c/mm23df2/us-cellular" class="company-name">
<h1 class="profile-company_name" itemprop="name">US Cellular</h1>
</a>
</div>
<div class="addr addr-co-header-gamma" itemprop="address"itemscope=""itemtype="http://schema.org/PostalAddress">
<em>United States Cellular Corporation</em>
<div class="company-address">
<div itemprop="streetAddress">2401 12th Avenue NW # 104B</div>
<span class="addressLocality" itemprop="addressLocality">Ardmore</span>,
<span class="addressRegion" itemprop="addressRegion">OK</span>
<span class="addresspostalCode" itemprop="postalCode">73401-1471</span>
</div>
<dl class="phone_info"><dt>Phone:</dt>
<dd class="tel" itemprop="telephone">(580) 490-3333</dd>
...
有人能帮我吗?谢谢。您需要提供有关所获得异常的更多信息(例如在哪一行引发异常),但是 如果未找到与XPath表达式匹配的项,则
SelectNodes
方法将返回null
,这意味着您必须在遍历节点之前检查返回值是否为null
。比如:
var companyAddressNodes = hDoc.DocumentNode.SelectNodes("//div[@itemprop='company-address']");
if (companyAddressNodes == null) {
//Throw properly exception here, log the error, or do anything you want...
throw new Exception("No company address node found. Perhaps the page layout changed?");
}
foreach (HtmlNode hNode in )
{
Response.Write(hNode.InnerText.ToString());
}
你能提供完整的stacktrace吗?我试过了,但找不到任何解决方案,这就是为什么贴在这里也许这有帮助:可能的重复
"Object reference not set to an instance of an object"
var companyAddressNodes = hDoc.DocumentNode.SelectNodes("//div[@itemprop='company-address']");
if (companyAddressNodes == null) {
//Throw properly exception here, log the error, or do anything you want...
throw new Exception("No company address node found. Perhaps the page layout changed?");
}
foreach (HtmlNode hNode in )
{
Response.Write(hNode.InnerText.ToString());
}