Detailed introduction to the case of C# using Selenium+PhantomJS to capture data (pictures and texts)

黄舟
Release: 2017-03-21 11:37:06
Original
1720 people have browsed it

This article mainly introduces the methods and steps of C# using Selenium+PhantomJS to capture data. It has a good reference value. Let’s take a look at it with the editor.

Needs for the project at hand Capture data from a website rendered with js. The page captured using the commonly used httpclient has no data. After searching on Baidu, the solution recommended by everyone is to use PhantomJS. PhantomJS is a webkit browser without an interface, which can use js to render pages with the same effect as the browser. Selenium is a web testing framework. Using Selenium to operate PhantomJS is a perfect match. But most of the examples on the Internet are in Python. Helpless, I downloaded python and followed the tutorial, but got stuck on the Selenium import problem. So I gave up and decided to use my usual C#, because I didn’t believe it was not available in C#. After half an hour of fiddling, I got it done (an hour of fiddling with python). Record this blog post so that novices in c# like me can use PhantomJS.

Step one: Open visual studio 2017, create a new console project, and open the nuget package manager.

Part 2: Search for Selenium and install Selenium.WebDriver. Note: If you want to use a proxy, it is best to install version 3.0.0.

Step 3: Write the code as shown below. But an error will be reported when executing. The reason is that PhantomJS.exe cannot be found. At this time, you can download one, or you can continue to step four.

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using System;
namespace ConsoleApp1
{
 class Program
 {
 static void Main(string[] args)
 {
  var url = "http://www.baidu.com";
  IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService());
  driver.Navigate().GoToUrl(url);
  Console.WriteLine(driver.PageSource);
  Console.Read();
 }
 private static PhantomJSDriverService GetPhantomJSDriverService()
 {
  PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService();
  //设置代理服务器地址
  //pds.Proxy = $"{ip}:{port}"; 
  //设置代理服务器认证信息
  //pds.ProxyAuthentication = GetProxyAuthorization();
  return pds;
 }
 }
}
Copy after login

Step 4: Open nuget to install the Selenium.PhantomJS.WebDriver package.

Step 5: Run. You can see that phantomjs.exe is automatically downloaded.

#Okay, now you can start your data capture business.

The above is the detailed content of Detailed introduction to the case of C# using Selenium+PhantomJS to capture data (pictures and texts). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template