469,592 Members | 1,670 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

How to display web search results with links by some WPF program?

Hi everyone,

I want to create a WPF app that could display web search results with active links to sites, like Bing. I'm using a headless browser PhantomJS for scraping data from Qwant Lite's search results.

Now on the app's window is text which contains only a few results, for example:

Forest - Wikipedia (website's title - it's not connected with hyperlink, but should be)
https://en.wikipedia.org/wiki/Forest (URL)
A forest is a large area dominated by trees. Hundreds of more precise definitions of forest are used throughout the world, incorporating factors such as tree density, tree height, land use, legal standing and ecological function. (site's description)

I have noticed that program displays all found websites' titles (along with URLs and descriptions) when they haven't NavigateUri property. They aren't however references to websites.

How to display all results found by PhantomJS with links?

Expand|Select|Wrap|Line Numbers
  1. bool search_end = false, page_se = false;
  2. byte page = 0; // Search result's index
  3. while (!search_end)
  4. {
  5.     try
  6.     {
  7.         while (!page_se)
  8.         {
  9.             try
  10.             {
  11.                 Run title = new Run(phantomDriver.FindElements(By.ClassName("result")).ElementAt(page).FindElement(By.ClassName("title")).Text); // Site's title
  12.                 Hyperlink hyperlink = new Hyperlink(title) 
  13.                 {
  14.                     NavigateUri = new Uri(phantomDriver.FindElements(By.ClassName("result")).ElementAt(page).FindElement(By.ClassName("url")).Text)
  15.                 };
  16.                 Results.Inlines.Add(hyperlink); // Results - TextBlock's name | Adding website's title connected to hyperlink
  17.                 Results.Text += Environment.NewLine;
  18.                 Results.Text += phantomDriver.FindElements(By.ClassName("result")).ElementAt(page).FindElement(By.ClassName("url")).Text + Environment.NewLine; // Site's URL
  19.                 Results.Text += phantomDriver.FindElements(By.ClassName("result")).ElementAt(page).FindElement(By.ClassName("desc")).Text + Environment.NewLine + Environment.NewLine; // Site's description
  20.                 ++page; // Scraping data from next result
  21.             }
  22.             catch
  23.             {
  24.                 page_se = true; // Stop scraping data from current page 
  25.             }
  26.         }
  27.         phantomDriver.FindElement(By.ClassName("next")).Click(); // Navigate to next page of search results
  28.         page = 0;
  29.     }
  30.     catch // Closing PhantomJS
  31.     {
  32.         phantomDriver.Quit();
  33.         search_end = true;
  34.     }
  35. }
  36.  
Jan 23 '20 #1
0 1671

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Alexandre Plennevaux | last post: by
5 posts views Thread by George | last post: by
reply views Thread by =?Utf-8?B?Qm9ubmll?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.