2

Is it possible to scrape all the text from a site that was navigated to by WebBrowser control without looking at the source?

Fedor
  • 1,548
  • 3
  • 28
  • 38
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

3 Answers3

7

David Walker's method is great when one don't need any info from the header nor non main part of the webpage. if one need something outside inner text, there is only two options, one is to parse with "getElement". the other one is issue commands (Document.ExecCommand) to webbrowser to select all and copy to clipboard:

wb.Document.ExecCommand("SelectAll", false, null);
wb.Document.ExecCommand("Copy", false, null);

then finally string content=clipboard.getText();

Please note the spelling and syntax may not be correct, I'm recalling from my memory

gg89
  • 372
  • 3
  • 11
5
string browserContents = webBrowser.Document.Body.InnerText;
David Walker
  • 1,496
  • 2
  • 16
  • 19
  • 1
    Thanks for putting me onto the scent David. If you want to preserve the formatting like I do, use webBrowser.Document.Body.InnerHtml; – Skyfish Aug 10 '20 at 15:52
4

You use the DocumentText property or the WebBrowser control.

This property is what holds the HTML of the site you have navigated to.

Update: (following comments)

If you want to parse the HTML and get the text parts of it, I suggest you use the HTML Agility Pack.

Max von Hippel
  • 2,856
  • 3
  • 29
  • 46
Oded
  • 489,969
  • 99
  • 883
  • 1,009