简体   繁体   中英

How I can select data from a webpage using HTML Agility Pack, but account that the data might be null?

I am currently trying to pull data from a website, and often times the data within this div is pretty standard. Sometimes the ID entered to get the page is wrong, so it will show an error UPS page and return the value as null. I want to be able to detect when it is null so that I can handle it appropriately, but when I try to do this it gives me an error: NullReferenceException - Object reference not set to an instance of an object. What do I need to do to handle these nulls?

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("https://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=" + tracking);
var divData = doc.GetElementbyId("tt_spStatus");
if (divData.InnerText == "null")
{
    status = divData.InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "").Trim();
}


Console.WriteLine(status);

What do you mean by halding these nulls?

You can check if InnerText is null or empty by using string.IsNullOrEmpty(divData.InnerText)

But if it's null then you can't use .Replace

Did you debug through it? You need to check if divData is null before checking InnerText

Replace

var divData = doc.GetElementbyId("tt_spStatus");

with

var divData = doc.GetElementbyId("tt_spStatus");
if (divData == null) 
{
    // Handle your business
}

Also, did you actually mean to do:

divData.InnerText == "null"

or did u mean:

divData.InnerText == null
var divData = doc.GetElementbyId("tt_spStatus");
if (divData != null)
{
    if (divData.InnerText == null)
    {
        status = divData.InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "").Trim();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM