错误使用C#网络爬虫
问题描述:
能有人请帮助我这个的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;
}
}
}
答
GetNewLinks()
返回一组字符串(ISet<String>
),不仅是一个。所以如果你想分配一个字符串(String Links
),那么你必须从集合中选择一个字符串,例如使用First()
。
嗯,是的 - 你有一个方法返回一组字符串,而你试图将它分配给一个'String'类型的变量。你是如何期待*这种工作的? –
是这功课吗? – mortb
它不是家庭作业,它是我正在研究的个人项目... – Jaco