【问题标题】:Problems with HTMLDocumentHTMLDocument 的问题
【发布时间】:2013-09-18 03:03:19
【问题描述】:

我是这个网络爬行世界的新手。所以有人在任何网络应用程序上工作过网络爬网吗?如果有人使用 asp.net & C# not VB.NET windows form,我需要帮助。

我有一个带有 3 个文本框和一个按钮的默认网络表单,这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using System.IO;




public partial class _Default : System.Web.UI.Page
{
    String Rstring;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        WebRequest myWebRequest;
        WebResponse myWebResponse;
        String URL = TextBox1.Text;

        myWebRequest = WebRequest.Create(URL);
        myWebResponse = myWebRequest.GetResponse();//Returns a response from an Internet resource

        Stream streamResponse = myWebResponse.GetResponseStream();//return the data stream from the internet
        //and save it in the stream

        StreamReader sreader = new StreamReader(streamResponse);//reads the data stream
        Rstring = sreader.ReadToEnd();//reads it to the end
        String Links = GetContent(Rstring);//gets the links only

        TextBox2.Text = Rstring;
        TextBox3.Text = Links;
        streamResponse.Close();
        sreader.Close();
        myWebResponse.Close();

    }


    //public ISet<string> GetNewLinks(string content)
    //{
    //    Regex regexLink = new Regex("(?<=<a\\s*?href=(?:'|\"))[^'\"]*?(?=(?:'|\"))");

    //    ISet<string> newLinks = new HashSet<string>();
    //    foreach (var match in regexLink.Matches(content))
    //    {
    //        if (!newLinks.Contains(match.ToString()))
    //            newLinks.Add(match.ToString());
    //    }

    //    return newLinks;
    //}

    private String GetContent(String Rstring)
    {
        String sString = "";
        HTMLDocument d = new HTMLDocument();
        IHTMLDocument2 doc = (IHTMLDocument2)d;
        doc.write(Rstring);

        IHTMLElementCollection L = doc.links;

        foreach (IHTMLElement links in L)
        {
            sString += links.getAttribute("href", 0);
            sString += "/n";
        }
        return sString;
    }
}

【问题讨论】:

    标签: c# asp.net web-crawler


    【解决方案1】:

    我知道这是一个老问题,但它从未得到回答,所以这里什么都没有,也许这会对某人有所帮助。

    我设法使您的代码正常工作并通过更改您获取内容的方式获得所有链接:

        private string GetContent(String Rstring)
        {
            String sString = "";
            String temp = "";
            mDocument.LoadHtml(Rstring);
            IEnumerable<HtmlNode> links = mDocument.DocumentNode.Descendants("a");
    
            foreach (HtmlNode link in links)
            {
                temp = link.GetAttributeValue("href", "");
                if (temp.StartsWith("https://") || temp.StartsWith("http://"))
                    sString += temp + "\n";
                else
                    continue;
            }
    
            return sString;
        }
    

    所以我去获取页面中的所有 a 元素,然后如果它们有一个有效的链接,我将它添加到字符串中并返回它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2014-04-27
      • 2011-10-04
      • 2011-06-23
      • 2010-11-21
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多