admin管理员组

文章数量:1332395

is there a way to fetch a webpage with HTTPWebRequest and then execute all the javascriptcode that was on the page along with ajax calls and save the result to a string?

I want to parse a website where some images are generated via js code and the images aren't on the webpage before the js script has run.

Now I searched the web for a solution to this but all I found were answers like this isn't possible, use the webbrowser instead which is not an option for me.

Greetings

is there a way to fetch a webpage with HTTPWebRequest and then execute all the javascriptcode that was on the page along with ajax calls and save the result to a string?

I want to parse a website where some images are generated via js code and the images aren't on the webpage before the js script has run.

Now I searched the web for a solution to this but all I found were answers like this isn't possible, use the webbrowser instead which is not an option for me.

Greetings

Share Improve this question asked Nov 16, 2012 at 19:34 maddo7maddo7 4,9837 gold badges32 silver badges54 bronze badges 3
  • 1 Can you elaborate on why using the WebBrowser class is not an option? – alexn Commented Nov 16, 2012 at 19:35
  • notions.okuda.ca/2009/06/11/… elaboration on what alexn is suggesting. I can't tell from your Q if you can't use the WebBrowser object if you can't use a web browser. – Eli Gassert Commented Nov 16, 2012 at 19:37
  • WebBrowser is slow and sometimes buggy, speed matters to me. – maddo7 Commented Nov 16, 2012 at 19:49
Add a ment  | 

3 Answers 3

Reset to default 3

You could do this with the C++ http system which gives you a lot of control over the pieces of data that are receievd, but I guess that's not really an answer so...

Why not try putting webkit into your app, and running off the events from it. It has several events that can notify you when downloads are started and finished.

I did a little research just because I thought it was interesting. I found something called Phantom.js which looks like it does what you need. Check it out: http://phantomjs/

This indeed isn't possible, as I found out while trying to meet a much-required demand. The Javascript will execute, but the Ajax calls won't be made.

The solution I settled for was to get all the data the Ajax calls would have synchronously, through the code-behind file(C# code), and to assign code-behind variables to Javascript variables, using script tags, and let Javascript do its work(building dynamic elements etc.)

本文标签: C Pull a webpage with HTTPWebRequest and execute the javascript from the siteStack Overflow