本程序基于CefSharp实现自动打开一个网站,自动输入账号密码自动注册。然后跳转到商品页自动输入评论的内容然后提交。完全模拟人为在浏览器的所有操作,包括自动输入,自动点击等操作。
本解决方案可以应用于网络爬虫,刷单,刷评论,参与自动秒杀活动。抢票程序等等。
最终效果如下:
cefsharp是一个在c#中使用Chrome浏览器内核实现浏览器功能的插件,类似于c#中的WebBrowser功能。
主要代码:
1、引用
2、执行JavaScript
public async void EvaluateJavaScript(string s, bool addCommonJs = false)
{
try
{
if (addCommonJs)
{
s = commonJs + s;
}
var response = await webBrowser.EvaluateScriptAsync(s);
if (response.Success && response.Result is IJavascriptCallback)
{
response = await ((IJavascriptCallback)response.Result).ExecuteAsync("This is a callback from EvaluateJavaScript");
}
var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}
catch (Exception e)
{
MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
3、设置cookie
public static void SetCefCookies(string url, CookieCollection cookies)
{
//Cef.GetGlobalCookieManager().SetStoragePath(Environment.CurrentDirectory, true);
foreach (System.Net.Cookie c in cookies)
{
var cookie = new CefSharp.Cookie
{
Creation = DateTime.Now,
Domain = c.Domain,
Name = c.Name,
Value = c.Value,
Expires = c.Expires
};
Task
while (!task.IsCompleted)
{
continue;
}
bool b = task.Result;
}
}
4、获取当前页面的html源码
private string GetHTMLFromWebBrowser()
{
// call the ViewSource method which will open up notepad and display the html.
// this is just so I can compare it to the html returned in GetSourceAsync()
// This is displaying all the html code (including child frames)
//webBrowser.GetBrowser().MainFrame.ViewSource();
// Get the html source code from the main Frame.
// This is displaying only code in the main frame and not any child frames of it.
Task
string response = taskHtml.Result;
return response;
}
5、拦截json请求结果
public class DefaultResourceHandler : ResourceRequestHandler
{
public event EventHandler
private Dictionary
public DefaultResourceHandler() { }
public DefaultResourceHandler(EventHandler
this.json_response_handler = json_response_handler;
}
protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
if (response.MimeType.Equals("application/json", StringComparison.OrdinalIgnoreCase)
|| (response.Headers["Content-Type"] != null && response.Headers["Content-Type"].ToLower().Contains("application/json")))
{
return JsonResponseFilter.CreateFilter(request.Identifier.ToString());
}
return null;
}
protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
var filter = JsonResponseFilter.GetFileter(request.Identifier.ToString()) as JsonResponseFilter;
if (filter != null)
{
var encode = !string.IsNullOrEmpty(response.Charset)
? Encoding.GetEncoding(response.Charset)
: Encoding.UTF8;
using (var read = new StreamReader(filter.GetStream(), encode))
{
//获取到的json内容
var text = read.ReadToEnd();
json_response_handler?.Invoke(response, new JsonResponseHandlerEventArgs(request, response, text));
Trace.WriteLine(response.MimeType + "=>" + request.Url + "::" + text);
}
}
}
}