我成功地在控制台应用中使用了scrapysharp。
我在VS2013中创建了一个新的MVC Web应用程序,没有身份验证或其他任何特殊功能。我使用nuget添加ScrapySharp,然后将此代码放在我的Home Controller中。我对我的pageresult没有回应。有没有人有任何想法?如何使用scrapysharp在MVC Web应用程序中工作?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrapySharp.Html.Forms;
using ScrapySharp.Network;
using ScrapySharp;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
namespace scrapyB.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var browser = new ScrapingBrowser();
browser.AllowAutoRedirect = true;
browser.AllowMetaRedirect = true;
var pageresult = browser.NavigateToPage(new Uri("https://google.com"));
var x = pageresult;
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
修改 实际上我只是尝试了一个Windows窗体应用程序,NavigateToPage也没有返回结果。这很奇怪,但它似乎只适用于控制台应用程序。有谁知道如何让ScrapySharp在控制台应用程序以外的其他地方工作?
答案 0 :(得分:3)
好的,所以scrapysharp有一个NavigateToPageAsync函数。这会按预期返回结果。
Form.submit()也使用NavigateToPage,但您可以使用NavigateToPageAsync并传入正确的参数来提交表单。