C# 用C语言对JavaScript生成的网页进行抓取#

C# 用C语言对JavaScript生成的网页进行抓取#,c#,javascript,html,visual-studio,web-scraping,C#,Javascript,Html,Visual Studio,Web Scraping,我有一个web浏览器,在Visual Studio中有一个标签,基本上我要做的就是从另一个网页上抓取一个部分 我尝试使用WebClient.DownloadString和WebClient.DownloadFile,它们都在JavaScript加载内容之前为我提供了网页的源代码。我的下一个想法是使用web浏览器工具,只需调用webBrowser.DocumentText,在加载页面后,它仍然会提供页面的原始来源 有没有办法抓取页面帖子JavaScriptload?问题是浏览器通常会执行JavaS

我有一个web浏览器,在Visual Studio中有一个标签,基本上我要做的就是从另一个网页上抓取一个部分

我尝试使用
WebClient.DownloadString
WebClient.DownloadFile
,它们都在JavaScript加载内容之前为我提供了网页的源代码。我的下一个想法是使用web浏览器工具,只需调用
webBrowser.DocumentText
,在加载页面后,它仍然会提供页面的原始来源


有没有办法抓取页面帖子
JavaScript
load?

问题是浏览器通常会执行JavaScript,结果是更新的DOM。除非您能够分析javascript或截获它使用的数据,否则您将需要像浏览器一样执行代码。在过去,我遇到过同样的问题,我使用selenium和PhantomJS来呈现页面。在它呈现页面之后,我将使用WebDriver客户端导航DOM并检索我需要的内容,发布AJAX

在高层,以下是步骤:

  • 已安装的selenium:
  • 已将selenium hub作为服务启动
  • 下载的phantomjs(无头浏览器,可以执行javascript):
  • 在指向selenium集线器的webdriver模式下启动phantomjs
  • 在我的抓取应用程序中安装了webdriver客户端nuget软件包:
    Install package Selenium.webdriver
  • 以下是phantomjs webdriver的示例用法:

    var options = new PhantomJSOptions();
    options.AddAdditionalCapability("IsJavaScriptEnabled",true);
    
    var driver = new RemoteWebDriver( new URI(Configuration.SeleniumServerHub),
                        options.ToCapabilities(),
                        TimeSpan.FromSeconds(3)
                      );
    driver.Url = "http://www.regulations.gov/#!documentDetail;D=APHIS-2013-0013-0083";
    driver.Navigate();
    //the driver can now provide you with what you need (it will execute the script)
    //get the source of the page
    var source = driver.PageSource;
    //fully navigate the dom
    var pathElement = driver.FindElementById("some-id");
    
    有关selenium、phantomjs和webdriver的更多信息,请访问以下链接:

    编辑:更简单的方法

    phantomjs似乎有一个nuget包,这样您就不需要集线器了(我使用集群以这种方式进行大规模的报废):

    安装web驱动程序:

    Install-Package Selenium.WebDriver
    
    安装嵌入式exe:

    Install-Package phantomjs.exe
    
    更新代码:

    var driver = new PhantomJSDriver();
    driver.Url = "http://www.regulations.gov/#!documentDetail;D=APHIS-2013-0013-0083";
    driver.Navigate();
    //the driver can now provide you with what you need (it will execute the script)
    //get the source of the page
    var source = driver.PageSource;
    //fully navigate the dom
    var pathElement = driver.FindElementById("some-id");
    

    好的,我将向您展示如何使用phantomjs和selenuim以及c#来启用javascript

  • 创建一个新的控制台项目,根据需要命名它
  • 转到右手中的解决方案资源管理器
  • 右键单击引用,然后单击管理NuGet软件包
  • windows将显示单击浏览而不是安装Selenium.WebDriver
  • 从这里下来的幻影
  • 在主函数中键入以下代码

        var options = new PhantomJSOptions();
        options.AddAdditionalCapability("IsJavaScriptEnabled", true);
        IWebDriver driver = new PhantomJSDriver("phantomjs Folder Path", options);
        driver.Navigate().GoToUrl("https://www.yourwebsite.com/");
    
        try
        {
            string pagesource = driver.PageSource;
            driver.FindElement(By.Id("yourelement"));
            Console.Write("yourelement founded");
    
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
    
        }
    
        Console.Read();
    
  • 别忘了在下面的代码中输入你的网站和你想要的元素,以及机器中的phantomjs.exe路径


    我发现,非常感谢
    wbennet
    ,祝您编码愉快。足够的免费服务可以通过web
    API
    调用废弃页面

    公共静态字符串GetPagePhantomJs(字符串url)
    {
    使用(var client=new System.Net.Http.HttpClient())
    {
    client.DefaultRequestHeaders.ExpectContinue=false;
    var pageRequestJson=new System.Net.Http.StringContent
    (@“{'url':'”+url+“,'renderType':'html','outputAsJson':false}”);
    var response=client.PostAsync
    ("https://PhantomJsCloud.com/api/browser/v2/{YOUT_API_KEY}/“,
    pageRequestJson)结果;
    返回response.Content.ReadAsStringAsync().Result;
    }
    }
    

    是的。

    谢谢你给我举个完美的例子。很好。我试过了,但驱动程序。Url不变。甚至不要例外。只要通过,它仍然是动作:空白。所以像这样导航返回。我试了另一个网址。请稍等,然后转到下一行。当然,代码末尾找不到一些id。有什么想法吗?服务很好,但是速度很慢