我在配置我正在为我目前正在进行的项目工作的网络抓取工具时遇到问题
我正在尝试从页面中抓取一系列链接,以评估我想要处理的链接。这是我的代码:
public partial class Form1 : Form
{
private byte[] aRequestHTML;
private string sourceString = null;
string[] a;
WebClient objWebClient = new WebClient();
LinkScraper linkScraper = new LinkScraper();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ScrapeLinks(textBox1.Text);
}
public void ScrapeLinks(string sourceLink)
{
// gets the HTML from the url written in the textbox
aRequestHTML = objWebClient.DownloadData(sourceLink);
// creates UTf8 encoding object
UTF8Encoding utf8 = new UTF8Encoding();
// gets the UTF8 encoding of all the html we got in aRequestHTML
sourceString = utf8.GetString(aRequestHTML);
// this is a regular expression to check for the urls
Regex r = new Regex("\\<a\\shref\\=(.*)\\>(.*)\\<\\/a\\>");
// get all the matches depending upon the regular expression
MatchCollection mcl = r.Matches(sourceString);
a = new string[mcl.Count];
int i = 0;
foreach (Match ml in mcl)
{
// Add the extracted urls to the array list
a[i] = ml.ToString();
Console.WriteLine(a[i]);
i++;
}
dataGridView1.DataSource = a;
// binds the databind
// The following lines of code writes the extracted Urls to the file named test.txt
StreamWriter sw = new StreamWriter("test.txt");
foreach (string aElement in a)
{
sw.Write(aElement + "\n");
}
sw.Close();
}
}
我的问题来自设置我的数据网格数据源。不是用字符串列表填充数据网格,而是用每个字符串长度填充。正如您将看到的,我有一个 test.txt 文件写出来,看看我是否在做一些愚蠢的事情,但文本文件包含每个字符串,因为我希望在数据网格中看到它
我已经在论坛上搜寻了 12 小时以寻求解决方案,但没有任何乐趣
有人可以告诉我为什么 .Value 没有将我的字符串返回到字符串数组“a”以绑定到数据网格吗?
一如既往地非常感谢任何帮助
问候巴里