第一步:打开visual studio 2017 新建一个控制台项目,打开nuget包管理器。
第二部:搜索selenium,安装selenium.webdriver。注意:如果要使用代理的话最好安装3.0.0版本。
第三步:写下如下图所示的代码。但是执行的时候会报错。原因是找不到phantomjs.exe。这时候可以去下载一个,也可以继续看第四步。
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; } } }
第四步:打开nuget安装selenium.phantomjs.webdriver包。
第五步:运行。可以看到phantomjs.exe被自动下载了。
好了,这样就可以开始你的数据抓取大业了。
以上就是c#使用selenium+phantomjs抓取数据详解的详细内容。
