【问题标题】:Console doesn't write any value控制台不写入任何值
【发布时间】:2019-09-20 20:58:18
【问题描述】:

我正在尝试抓取 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_scraping
{
    /// <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 Scrape()
        {
            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)
        {
            Scrape();

        }


    }

}

我希望 title,price,rate 的输出为文本 你好世界 50 三,或类似的东西

【问题讨论】:

  • 这看起来不像是控制台应用程序。
  • 但是您何时将.Add() 加入books 列表?它已声明但未使用。
  • 您没有向books 添加任何书...此外,您的第三个循环可能也需要移到外面...否则没有理由创建一个您将循环遍历的列表打印到控制台。

标签: c# selenium web-scraping


【解决方案1】:

你的书单是空的,试试这个

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 ", ""),

});

代替:

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 ", ""),

};

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2013-07-22
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多