我在C#中使用Web抓取和html敏捷包出错 [英] i have error with web scraping and html agility pack in c#

查看:44
本文介绍了我在C#中使用Web抓取和html敏捷包出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private void button1_Click(object sender, EventArgs e)
    {
        var html = @"https://html-agility-pack.net/from-web";
        HtmlWeb web = new HtmlWeb();
        var htmldoc = web.Load(html);
        var nod = htmldoc.DocumentNode.SelectSingleNode("//head/title");
        textBox1.Text = "Node Name: " + nod.Name + "\n" + nod.OuterHtml;
    }

起初我想使用c#和html敏捷包学习网络爬虫,但是当我运行我的第一个代码时,将出现以下消息:

at first i want learning webscraoing with c# and html agility pack but when I run my first code this meassage will appear:

{类型为'System.Net.WebException'的未处理异常发生在 HtmlAgilityPack.dll

{An unhandled exception of type 'System.Net.WebException' occurred in HtmlAgilityPack.dll

其他信息:基础连接已关闭:An 发送时发生意外错误.}

Additional information: The underlying connection was closed: An unexpected error occurred on a send.}

推荐答案

基础连接已关闭:发送时发生意外错误

当客户端计算机由于连接已关闭或不可用而无法发送HTTP请求时,会发生此问题.

This problem occurs when the client computer cannot send an HTTP request because the connection has been closed or unavailable.

由于您尝试访问HTTPS页面,因此可能必须在创建请求之前设置安全协议.

Since you're trying to accessing HTTPS page you may had to set security protocol before creating request.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var html = @"https://html-agility-pack.net/from-web";
HtmlWeb web = new HtmlWeb();

这将首选TLS 1.2,但仍允许使用1.1和1.0(以防止失败,因为某些站点不提供TLS 1.2).

This will prefer TLS 1.2 but still allow 1.1 and 1.0 (to prevent failure because some sites doesn't offer TLS 1.2).

这篇关于我在C#中使用Web抓取和html敏捷包出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆