Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTMLDocument的问题_C#_Asp.net_Web Crawler - Fatal编程技术网

C# HTMLDocument的问题

C# HTMLDocument的问题,c#,asp.net,web-crawler,C#,Asp.net,Web Crawler,我是这个网络爬行世界的新手。那么有人为网络爬网开发过任何网络应用程序吗?如果有人使用asp.net&C而不是VB.net windows窗体,我需要帮助 我有一个带有3个文本框和一个按钮的默认webform,下面是代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

我是这个网络爬行世界的新手。那么有人为网络爬网开发过任何网络应用程序吗?如果有人使用asp.net&C而不是VB.net windows窗体,我需要帮助

我有一个带有3个文本框和一个按钮的默认webform,下面是代码:

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
Net系统;
使用System.IO;
公共部分类\u默认值:System.Web.UI.Page
{
字符串Rstring;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
WebRequest-myWebRequest;
WebResponse-myWebResponse;
字符串URL=TextBox1.Text;
myWebRequest=WebRequest.Create(URL);
myWebResponse=myWebRequest.GetResponse();//返回来自Internet资源的响应
streamResponse=myWebResponse.GetResponseStream();//从internet返回数据流
//并将其保存在流中
StreamReader sreader=newstreamreader(streamResponse);//读取数据流
Rstring=sreader.ReadToEnd();//读取到最后
String Links=GetContent(Rstring);//仅获取链接
TextBox2.Text=Rstring;
TextBox3.Text=链接;
streamResponse.Close();
sreader.Close();
myWebResponse.Close();
}
//公共ISet GetNewLinks(字符串内容)
//{

//Regex regexLink=new Regex(“(?我知道这是一个老问题,但从来没有得到回答,所以这里什么都没有,也许这会帮助某人

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

    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;
    }
private string GetContent(string Rstring)
{
字符串sString=“”;
字符串temp=“”;
mDocument.LoadHtml(Rstring);
IEnumerable links=mDocument.DocumentNode.substands(“a”);
foreach(链接中的HtmlNode链接)
{
temp=link.GetAttributeValue(“href”,”);
if(临时启动带(“https:/”)| |临时启动带(“http:/”)
sString+=temp+“\n”;
其他的
继续;
}
返回字符串;
}
因此,我获取页面中的所有a元素,然后如果它们有有效链接,我将其添加到字符串中并返回它