2015-09-25 68 views
-2

能有人請幫助我這個的WebCrawler,我不斷收到錯誤:錯誤使用C#網絡爬蟲

無法隱式轉換 型「System.Collections.Generic.ISt」到「字符串。

此錯誤是符合它在哪裏String Links = GetNewLinks(Rstring);,能有人幫,這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Net; 
using System.IO; 
using System.Text.RegularExpressions; 



namespace Crawler 
{ 
public partial class Crawler : Form 
{ 
    String Rstring; 

    public Crawler() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     WebRequest myWebRequest; 
     WebResponse myWebResponse; 
     String URL = txt1.Text; 

     myWebRequest = WebRequest.Create(URL); 
     myWebResponse = myWebRequest.GetResponse(); 

     Stream streamResponse = myWebResponse.GetResponseStream(); 

     StreamReader sreader = new StreamReader(streamResponse); 
     Rstring = sreader.ReadToEnd(); 
     String Links = GetNewLinks(Rstring); 

     txt2.Text = Rstring; 
     txt3.Text = Links; 
     sreader.Close(); 
     streamResponse.Close(); 
     myWebResponse.Close(); 

    } 

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

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

     return newLinks; 
    } 
} 
} 
+1

嗯,是的 - 你有一個方法返回一組字符串,而你試圖將它分配給一個'String'類型的變量。你是如何期待*這種工作的? –

+0

是這功課嗎? – mortb

+0

它不是家庭作業,它是我正在研究的個人項目... – Jaco

回答

0

GetNewLinks()返回一組字符串(ISet<String>),不僅是一個。所以如果你想分配一個字符串(String Links),那麼你必須從集合中選擇一個字符串,例如使用First()