我想从某些确实具有用户名和密码的网站上抓取数据。我正在使用webclient,但它不接受登录。 我可以抓取数据,但是数据说要登录才能查看功能。它没有显示所需数据。
我也尝试过凭据缓存,但是不起作用 我可以在chromedriver的帮助下完成此操作,但是这非常耗时,因为我正在动态创建url并将其加载到chromedriver中,然后抓取数据。动态URL有时可以是1000。每次在chromedriver中加载动态URL,然后对其进行绉纹处理时,SO都不可行。
场景1:
using (var client = new CookieAwareWebClient())
{
var values = new NameValueCollection
{
{ "username", "username" },
{ "password", "pwd" },
};
client.UploadValues("url", values);
string result = client.DownloadString("url");
方案2:
var webclient = new System.Net.WebClient();
CredentialCache credCache = new CredentialCache();
var creds = new System.Net.NetworkCredential("username", "passwd");
credCache.Add(uri, "Basic", creds);
webclient.Credentials = credCache;
var webpage = webclient.DownloadString(url);
方案3:
WebRequest request = WebRequest.Create("url");
request.Credentials = new NetworkCredential("username", "passwd");
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
方案4:
WebClient webClient = new WebClient();
webClient.Credentials = new System.Net.NetworkCredential("UserName",
"Password", "Domain");
在上述所有4种情况下,返回的数据均显示“登录以查看此功能”