控制台未写入任何值

时间:2019-09-20 20:58:18

标签: c# selenium web-scraping

我正在尝试抓取books.toscrape.com一切似乎都很完美,但没有输出任何内容给控制台。

我确定XPath正确,语法正确。

我没有看到任何错误或警告。

没有任何线索可以解决这个问题。

using System;
using System.Windows;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Data;
using System.Collections.Generic;

namespace book_scrapping
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {



        DataTable table = new DataTable();
        public MainWindow()
        {
            InitializeComponent();

        }

        string user_url;
        class Book
        {

            public string Titlex { get; set; }
            public string Price { get; set; }
            public string Rate { get; set; }


        }


        public void Scrap()
        {
            var books = new List<Book>();
            IWebDriver driver = new FirefoxDriver();
            user_url = Textbox1.Text;
            int.TryParse(Textbox2.Text, out var x);
            for (int i = 1; i < x; i++)
            {
                driver.Url = "http://" + user_url + "/catalogue/" + "page-" + i + ".html";
                var element = driver.FindElements(By.XPath("//article[@class='product_pod']"));
                foreach (var elements in element) {

                    var book = new Book
                    {
                        Titlex = driver.FindElement(By.XPath("//h3/a")).Text,
                        Price = driver.FindElement(By.XPath("//p[@class='price_color']")).Text,
                        Rate = driver.FindElement(By.XPath("//article/p")).GetAttribute("class")?.Replace("star-rating ", ""),

                    };
                    foreach (var a in books)
                    {

                        Console.WriteLine($"{a.Titlex} {a.Price} {a.Rate}");

                   }

                }


            }

        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Scrap();

        }


    }

}

我希望标题,价格,费率的输出像文本一样 你好世界50三,或类似的东西

1 个答案:

答案 0 :(得分:0)

您的图书列表为空,请尝试

client.on('message', message => {
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
       if (command == 'help') {
        client.commands.get('help').execute(message, args);
    }
});

而不是:

books.Add(new Book
{
   Titlex = driver.FindElement(By.XPath("//h3/a")).Text,
   Price = driver.FindElement(By.XPath("//p[@class='price_color']")).Text,
   Rate = driver.FindElement(By.XPath("//article/p")).GetAttribute("class")?.Replace("star-rating ", ""),

});