3

我是scrapySharp以及网络抓取的新手。我正在尝试抓取一个安全且具有登录屏幕的站点。form 元素没有 name/id 属性,因此使我的生活更加复杂。我一直无法弄清楚如何使用下面的代码加载表单。非常感谢任何见解!

C#:

ScrapingBrowser browser = new ScrapingBrowser();
var homepage = browser.NavigateToPage(new Uri("https://somedomain.com/ProviderLogin.action/"));
var form1 = homepage.Find("form", ScrapySharp.Html.By.Text("form"));
var form2 = homepage.FindFormById("form[action='provider-login']");

HTML:

   <form action="provider-login" method="post">           
        <div class="login-box">   
            <input type="text" name="username" id="username" autocomplete="false" placeholder="Username" 
                   class="form-control input-lg login-input login-input-username" value="" />                   
            <input type="password" id="password" name="password" placeholder="Password" type="password"
             class="form-control input-lg login-input login-input-password" />
            <button name="login" type="submit" class="btn btn-primary btn-block btn-md login-btn" >
                Login
            </button>            
        </div>
    </form>
4

2 回答 2

1

您无法在 ScrapySharp 中使用“By”来实现这一点,因为它只有四种“元素搜索类型”:

{
   Text,
   Id,
   Name,
   Class
}

在您的情况下,您没有其中之一,因此请考虑使用“CssSelect”来实现您的目的:

var form = homepage.Html.CssSelect("form[action='provider-login']");
//Or
var form = homepage.Html.CssSelect("form[action*='provider-login']");
于 2019-12-05T11:26:26.853 回答
1

您可以通过标签找到第一个表单节点,然后使用PageWebForm 构造函数

var browser = new ScrapingBrowser();
var homepage = browser.NavigateToPage(new Uri("https://somedomain.com/ProviderLogin.action/"));

var form1node = homepage.Html.SelectSingleNode("//form");
var form1 = new PageWebForm(form1node, browser); // this is where it happens!

form1["username"] = "some username";
form1["password"] = "some password";
form1.Method = HttpVerb.Post;
var webpage = form1.Submit();
于 2020-01-22T22:55:03.900 回答