如何让 scrapysharp 在 MVC 网络应用程序中工作?
How do I get scrapysharp to work in a MVC web app?
我成功地让 scrapysharp 在控制台应用程序中工作。
我在 VS2013 中创建了一个新的 MVC 网络应用程序,没有身份验证或任何其他特殊内容。我使用 nuget 添加了 ScrapySharp,然后在我的 Home Controller 中添加了这段代码。我的页面结果没有任何回应。有谁知道为什么?如何让 scrapysharp 在 MVC 网络应用程序中工作?
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 Forms 应用程序,而 NavigateToPage 也没有 return 结果。这很奇怪,但它似乎只适用于控制台应用程序。有谁知道如何让 ScrapySharp 在控制台应用程序以外的地方工作?
好的,scrapysharp 有一个 NavigateToPageAsync 函数。这 returns 符合预期的结果。
Form.submit() 也使用 NavigateToPage,但您可以改用 NavigateToPageAsync 并传入正确的参数以提交表单。
我成功地让 scrapysharp 在控制台应用程序中工作。
我在 VS2013 中创建了一个新的 MVC 网络应用程序,没有身份验证或任何其他特殊内容。我使用 nuget 添加了 ScrapySharp,然后在我的 Home Controller 中添加了这段代码。我的页面结果没有任何回应。有谁知道为什么?如何让 scrapysharp 在 MVC 网络应用程序中工作?
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 Forms 应用程序,而 NavigateToPage 也没有 return 结果。这很奇怪,但它似乎只适用于控制台应用程序。有谁知道如何让 ScrapySharp 在控制台应用程序以外的地方工作?
好的,scrapysharp 有一个 NavigateToPageAsync 函数。这 returns 符合预期的结果。
Form.submit() 也使用 NavigateToPage,但您可以改用 NavigateToPageAsync 并传入正确的参数以提交表单。