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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Zoran HorvatPosted Aug 5, 2011, 6:13 AM
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
Sam HobbsPosted Aug 5, 2011, 5:16 PM
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.
Janis PeksaPosted Aug 5, 2011, 6:30 AM
Zoran HorvatPosted Aug 5, 2011, 6:10 AM
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 PeksaPosted Aug 5, 2011, 6:00 AM
Janis PeksaPosted Aug 5, 2011, 5:46 AM
Zoran HorvatPosted Aug 5, 2011, 5:43 AM
Zoran
Janis PeksaPosted Aug 5, 2011, 5:40 AM
Zoran HorvatPosted Aug 5, 2011, 5:36 AM
Zoran