Scraping webpage generated by javascript with C#

前端 未结 3 1605
深忆病人
深忆病人 2020-11-27 03:47

I have a webBrowser, and a label in Visual Studio, and basically what I\'m trying to do is grab a section from another webpage.

I tried using WebClient.DownloadStrin

3条回答
  •  一整个雨季
    2020-11-27 04:44

    ok i will show you how to enable javascript using phantomjs and selenuim with c#

    1. create a new console project name it as you want
    2. go to solution explorer in your right hand
    3. a right click on References click on Manage NuGet packages
    4. a windows will shows click on browse than install Selenium.WebDriver
    5. downold phantomjs from here Phantomjs
    6. in your main function type this code

          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();
      

    don't forget to put yourwebsite and the element that you loooking for and the phantomjs.exe path in you machine in this code below

    have great time of coding and thanks wbennett

提交回复
热议问题