Hello, I need to create app that can find text in web page like "summer" and put in string.
Can someone please tell me how can I do that ?
Maybe some samples please.
Best regards.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 5, 2011, 2:13 AM EDT
http://htmlagilitypack.codeplex.com/
After that you would simply have to iterate through the object model of the HTML page downloaded and to extract raw text, i.e. to strip off all formatting contained in the HTML.
Zoran
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 5, 2011, 1:36 AM EDT
Zoran
Janis Peksa145 reputationPosted Aug 5, 2011, 1:40 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 5, 2011, 1:43 AM EDT
Zoran
Janis Peksa145 reputationPosted Aug 5, 2011, 1:46 AM EDT
Janis Peksa145 reputationPosted Aug 5, 2011, 2:00 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 5, 2011, 2:10 AM EDT
Here is the source code which opens a page from Google:
using System.Net;
using System;
using System.IO;
namespace ExceptionTest
{
class Program
{
static void Main(string[] args)
{
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com/search?hl=en&source=hp&biw=1680&bih=917&q=search+something&oq=search+something&aq=f&aqi=g7g-v3&aql=&gs_sm=e&gs_upl=2820l4595l0l4723l16l11l0l3l3l1l231l1188l2.4.2l8l0");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
string html = null;
if (resp.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
html = reader.ReadToEnd();
}
Console.WriteLine(html);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
}
}
}
After reading the response, string variable html contains raw HTML contents of the page downloaded. Then you can parse it and read its content to find the string.
Zoran
Janis Peksa145 reputationPosted Aug 5, 2011, 2:30 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Aug 5, 2011, 1:16 PM EDT
Alternatively you can use the InternetExplorer class. First you would add a handler for the DocumentComplete event. Then navigate to a web page.
Then you have two or more options. The most likely option would be to create an IHTMLTxtRange and then use the findText function. If you do that, then you would be using the same thing that IE uses to find text in a page. Alternatively, you could iterate through the text nodes.
Since you have already chosen Zoran's solution I won't dwell on the details of any of that. Note however that as an editor of articles in this web site, I have written tools to help me and one of the tools uses IHTMLTxtRange to find and replace text in articles. So I know it is possible.
IE 9 has a new way to iterate text nodes and I spent time trying to get it to work. No one knows why it does not work, but it is possible to use the same solutions that were used before IE 9.