【问题标题】:Web Scraper - Regex Match.Value returning string length not string itselfWeb Scraper - Regex Match.Value 返回字符串长度而不是字符串本身
【发布时间】:2012-06-03 09:13:03
【问题描述】:

我在为目前正在进行的项目配置网络抓取工具时遇到问题

我正在尝试从页面中抓取一系列链接,以评估我想要处理哪些链接。这是我的代码:

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' 以绑定到数据网格吗?

我们一如既往地非常感谢任何帮助

问候 巴里

【问题讨论】:

    标签: regex datagridview screen-scraping match


    【解决方案1】:

    刚刚找到解决方案的人

    DataGridView 显示它可以为字符串找到的第一个属性,即它的长度属性 解决方法是使用 DataTable

     DataTable links = new DataTable();
     links.Columns.Add("Link URL");
    
     foreach (Match ml in mcl)
     {
       // Add the extracted urls to table
       links.Rows.Add(new object[] {ml.Value});
     }
    

    【讨论】:

      【解决方案2】:

      您可以将页面转换为 XML,然后使用 XPath 和 JavaScript 的 E4X 来简化。

      查看 Script Scraper 我所做的。

      谢谢, 马丁

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 2020-10-15
        • 2019-09-15
        相关资源
        最近更新 更多