【发布时间】:2019-02-19 23:04:13
【问题描述】:
我需要在门户网站上解析损坏的链接(例如 404)。它在门户网站上有大约 10.000 个页面和许多外部链接。我在 c# 中使用 HttpWebRequest 来下载页面 html,然后在 html 中查找所有链接,并在已检查的组中添加已检查的链接,并在未检查的链接队列中进行检查。我使用多线程,但它几乎没有优势。 1 个线程 - 每分钟 50 个链接,5 个线程 - 70 个。 我想,我的问题是我获取 html 代码的方式。我使用简单的 HttpWebRequest 来获取 html:
`
string html;
HttpWebResponse response;
StreamReader respStream;
try
{
HttpWebRequest reques = (HttpWebRequest)WebRequest.Create(link.ToString());
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/15.0";
request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.AllowAutoRedirect = true;
response = (HttpWebResponse)request.GetResponse();
respStream = new StreamReader(response.GetResponseStream());
html = respStream.ReadToEnd();
response.Close();
respStream.Close();
}
catch (Exception ex)
{
System.Console.WriteLine("-------------\n" +
"Bad link: " + link + "\n" +
"From: " + link.Parent +
"\n" + ex.Message);
link.ErrorComments = ex.Message;
link.Parent.AddSon(link);
continue;
}
normalLinks.Add(link);
`
我的互联网 - 100mb/s。 1 页 - 150-200 kb。 我需要几次提高速度。有人知道解决方案吗?
【问题讨论】:
-
您好!欢迎来到堆栈溢出。感谢您发布详细的问题,展示您已经尝试过的内容! :)
-
也许只读取响应的标头而不是完整的响应?
-
我只得到标题,如果链接是外部的(不是来自主域)。但我需要完整的响应来解析内部页面的 html 以找到其他链接。
标签: c# parsing web web-scraping https