C# httpwebrequest and javascript

前端 未结 7 1952
北恋
北恋 2020-11-28 13:02

I am using C# HttpWebRequest to get some data of a webpage. The problem is that some of the data is updated using javascript/ajax after the page is loaded and I am not getti

7条回答
  •  旧时难觅i
    2020-11-28 13:22

    You could use of the PhantomJs. I had this Issue, but don't found solution for my problem. In my opinion, best solution is This.

    My solution is look like this:

    var page = require('webpage').create();
    
    page.open("https://sample.com", function(){
        page.evaluate(function(){
            var i = 0,
            oJson = jsonData,
            sKey;
            localStorage.clear();
    
            for (; sKey = Object.keys(oJson)[i]; i++) {
                localStorage.setItem(sKey,oJson[sKey])
            }
        });
    
        page.open("https://sample.com", function(){
            setTimeout(function(){
             page.render("screenshoot.png") 
                // Where you want to save it    
               console.log(page.content); //page source
                // You can access its content using jQuery
                var fbcomments = page.evaluate(function(){
                    return $("body").contents().find(".content") 
                }) 
                phantom.exit();
            },10000)
        });     
    });
    

提交回复
热议问题