There is a webpage I'm trying to extract data from. After looking at the HTML on the Source page, I can find the data that interests me in the script tags. It looks like this:
<html> <script type="text/javascript"> window.gon = {}; gon.default_profile_mode = false; gon.user = null; gon.product = "shoes"; gon.books_jsonarray = [ { "title": "Little Sun", "authors": [ "John Smith" ], edition: 2, year: 2009 }, { "title": "Little Prairie", "authors": [ "John Smith" ], edition: 3, year: 2009 }, { "title": "Little World", "authors": [ "John Smith", "Mary Neil", "Carla Brummer" ], edition: 3, year: 2014 } ]; </script> </html>
What I would like to achieve is to invoke a webpage using its url and then extract the gon variable from JavaScript and save it in a C # variable. In other words, in C #, I would like to have a data structure (like a dictionary) that will contain the value "gon".
I tried to figure out how to get a variable defined in JavaScript via C # WebBrowser, and here is what I found:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Net; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using mshtml; namespace Mynamespace { public partial class Form1 : Form { public WebBrowser WebBrowser1 = new WebBrowser(); private void Form1_Load(object sender, EventArgs e) { string myurl = "http://somewebsite.com";
I can get the values ββof strings or numeric variables, such as:
var mystr = "Hello Sir"; var mynbr = 8;
However, although I can see that the variable "gon" is passed as "{System .__ ComObject}", I donβt know how to parse it to see the values ββof its subcomponents, It would be nice if I could parse it, but if no, then I would like it to be a C # data structure with keys / values ββthat contains all the auxiliary data for the gon variable, and especially the ability to view the variable 'gon.books_jsonarray'.
Any help on how to achieve this would be greatly appreciated. Please note that I canβt change the source html / javascript in any way, and therefore I need C # code that will achieve my goal.