【问题标题】:Using Xpath to scrape links and descriptions from this Etsy product listings page使用 Xpath 从这个 Etsy 产品列表页面中抓取链接和描述
【发布时间】:2018-08-11 11:36:06
【问题描述】:

我正在尝试抓取https://www.etsy.com/market/happiness_bracelet 上的所有链接,然后从每个链接中提取产品描述。

我正在使用一个名为 Scraper 的 chrome 扩展来输入 xpath,它是通过右键单击 Chrome 开发工具中的元素获得的。但没有得到想要的结果。

问题:找不到链接的正确 xpath。获取该网页上链接的 xpath 并从其中提取产品描述的正确设置是什么?

有没有办法只使用 Chrome 开发工具和适当的 Xpath,或者我需要 Python/bs4/selenium 来完成这项任务?

感谢您的帮助。

【问题讨论】:

    标签: python selenium xpath web-scraping google-chrome-devtools


    【解决方案1】:

    通过 Chrome Devtools 中的以下 XPath 表达式,您可以获得带有产品描述的所有 p 标签元素:

    //div[contains(@class,'v2-listing-card__info')]/div/p[contains(@class,'body')]
    

    查看结果图片:

    你找到了 C# 代码,用于从第一个手镯的概述段落中获取第一行文本('Handmade item')。

            // Go to the webpage
            Driver.Url = "https://www.etsy.com/market/happiness_bracelet";
            // Get all the links 
            IList<IWebElement> searchResults = Driver.FindElements(By.XPath("//a[contains(@class,'listing-link')]"));
            var i = 0;
            foreach (var result in searchResults)
            {
                i++;
                if (i > 5)
                // For all links except the first 5 hidden links
                {
                    // Click the link to go to the page with information of the bracelet
                    result.Click();
                    // Write the first line of text of the Overview paragraph to the consol
                    Console.WriteLine(Driver.FindElement(By.Id("item-overview")).FindElement(By.XPath(".//li")).Text);
                    // More code needed here to pick the other information you needed 
                }             
            }
    

    【讨论】:

    • 好的,我已将它们保存在一个列表中。 “循环元素”和“元素上的.text”是什么意思?我所指的产品描述是产品链接中的“概述”“运输和“退货”和“描述”部分。我要循环什么
    • 现在我知道您需要单击链接后到达的页面上的信息。为此,您需要锚标记。您可以为其使用以下 XPath: //a[contains(@class,'listing-link')]
    • 我添加了一些代码来解释“循环元素”的含义。
    • 我根本看不懂代码。您能解释一下我在哪里输入网址以获得所需的结果吗?我想要该产品列表页面上每个链接中的“概述”“运输”和“退货”“描述”部分。
    • 这个示例代码是关于在搜索结果页面上的图像下获取描述的,因为我最初以为你在寻找这些描述。您可以通过将代码示例中的 XPath 替换为我在之前的评论中给您的 XPath 来获取 URL。接下来,在代码示例中,您必须将 .Text 替换为 .Click() 才能转到您要查找的页面。这些只是达到目标的指示,显然需要更多代码才能完成工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多