Jon*_*ell 6 c# networkcredentials html-agility-pack
如果给定一个特定的url,我有一个获取id和xpath的方法.如何通过请求传递用户名和密码,以便我可以抓取需要用户名和密码的网址?
using HtmlAgilityPack;
_web = new HtmlWeb();
internal Dictionary<string, string> GetidsAndXPaths(string url)
{
var webidsAndXPaths = new Dictionary<string, string>();
var doc = _web.Load(url);
var nodes = doc.DocumentNode.SelectNodes("//*[@id]");
if (nodes == null) return webidsAndXPaths;
// code to get all the xpaths and ids
Run Code Online (Sandbox Code Playgroud)
我应该使用Web请求获取页面源,然后将该文件传递给上面的方法吗?
var wc = new WebClient();
wc.Credentials = new NetworkCredential("UserName", "Password");
wc.DownloadFile("http://somewebsite.com/page.aspx", @"C:\localfile.html");
Run Code Online (Sandbox Code Playgroud)
HtmlWeb.Load
有许多重载,它们接受实例,NetworkCredential
或者您可以直接传入用户名和密码。
Name // Description
Public method Load(String) //Gets an HTML document from an Internet resource.
Public method Load(String, String) //Loads an HTML document from an Internet resource.
Public method Load(String, String, WebProxy, NetworkCredential) //Loads an HTML document from an Internet resource.
Public method Load(String, String, Int32, String, String) //Loads an HTML document from an Internet resource.
Run Code Online (Sandbox Code Playgroud)
不需要传入实例WebProxy
,也可以传入系统默认的实例。
或者,您可以连接HtmlWeb.PreRequest
并设置请求的凭据。
htmlWeb.PreRequest += (request) => {
request.Credentials = new NetworkCredential(...);
return true;
};
Run Code Online (Sandbox Code Playgroud)