【问题标题】:Scrape a table using ScrapySharp and HtmlAgilityPack使用 ScrapySharp 和 HtmlAgilityPack 抓取表格
【发布时间】:2019-09-25 14:44:35
【问题描述】:

我正在尝试从特定网站抓取经济日历。实际上,我尝试了很多次都没有成功,我不知道我错在哪里。你能帮帮我吗?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Network;

namespace Calendar
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://www.fxstreet.com/economic-calendar";
            var webGet = new HtmlWeb();
            if (webGet.Load(url) is HtmlDocument document)
            {
                var nodes = document.DocumentNode.CssSelect("#fxst-calendartable tbody").ToList();
                foreach (var node in nodes)
                {
                    // event_time
                    Debug.Print(node.CssSelect("div div").Single().InnerText);
                    // event-title
                    Debug.Print(node.CssSelect("div a").Single().InnerText);
                }
            }
            Console.WriteLine("");
            Console.ReadLine()
        }
    }
}

【问题讨论】:

    标签: scrapysharp


    【解决方案1】:

    您遇到了什么错误? 如果您想从网站上发布活动名称和时间,我假设您需要阅读表格。 您可以使用

    HtmlNode tablebody = doc.DocumentNode.SelectSingleNode("//table[@class='fxs_c_table']/tbody");
        foreach(HtmlNode tr in tablebody.SelectNodes("./tr[@class='fxs_c_row']"))
        {
           Console.WriteLine("\nTableRow: ");
            foreach(HtmlNode td in tr.SelectNodes("./td"))
            {
               Console.WriteLine(td.SelectSingleNode("./span").InnerText);
            }
    
        }
    

    获取具有类属性的表,然后使用相关的 XPATH 遍历元素。请发布您在代码中遇到的错误。

    【讨论】:

    • 在你的答案中加入问题时要小心——它最终可能会被删除为“不是答案”。我认为这个很好,因为它确实提供了有用的建议。
    • @DavidW ,注意。但这个问题并没有说明用户面临什么错误或问题,即获取空值或获取一些异常。
    猜你喜欢
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2012-07-20
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多