【问题标题】:Scrapy and Selenium error : Element not found in the cache - perhaps the page has changed since it was looked up StacktraceScrapy 和 Selenium 错误:在缓存中找不到元素 - 页面可能在查找后已更改 Stacktrace
【发布时间】:2015-07-21 20:56:29
【问题描述】:

我想从亚马逊提取数据。
这是我的源代码:

    from scrapy.contrib.spiders import CrawlSpider
    from scrapy import Selector
    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    from time import sleep
    import selenium.webdriver.support.ui as ui
    from scrapy.xlib.pydispatch import dispatcher
    from scrapy.http import HtmlResponse, TextResponse
    from extraction.items import ProduitItem

    class RunnerSpider(CrawlSpider):
      name = 'products'
      allowed_domains = ['amazon.com']
      start_urls = ['http://www.amazon.com']

      def __init__(self):
         self.driver = webdriver.Firefox()

     def parse(self, response):
        items = []
        sel = Selector(response)
        self.driver.get(response.url)
        recherche = self.driver.find_element_by_xpath('//*[@id="twotabsearchtextbox"]')
        recherche.send_keys("A")
        recherche.submit()
        resultat = self.driver.find_element_by_xpath('//ul[@id="s-results-list-atf"]')
        resultas = resultat.find_elements_by_xpath('//li')
        for result in resultas:
          item = ProduitItem()
          lien = result.find_element_by_xpath('//div[@class="s-item-container"]/div/div/div[2]/div[1]/a')
          lien.click()
          #lien.implicitly_wait(2)
          res = self.driver.find_element_by_xpath('//h1[@id="aiv-content-title"]')
          item['TITRE'] = res.text
          item['IMAGE'] = lien.find_element_by_xpath('//div[@id="dv-dp-left-content"]/div[1]/div/div/img').get_attribute('src')
          items.append(item)

        self.driver.close()
        yield items

当我运行我的代码时,我得到了这个错误:

Element not found in the cache - perhaps the page has changed since it was looked up Stacktrace:

【问题讨论】:

  • 这个错误是由这些指令引起的:lien = result.find_element_by_xpath('//div[@class="s-item-container"]/div/div/div[2]/div[ 1]/a') lien.click() res = self.driver.find_element_by_xpath('//h1[@id="aiv-content-title"]') item['TITRE'] = res.text
  • 如果您有其他详细信息要添加,请edit 提出您的问题,而不是发表评论。
  • 好的,我遇到了问题,但有什么解决方案或帮助吗?

标签: python selenium scrapy amazon screen-scraping


【解决方案1】:

如果你告诉 Selenium 点击一个链接,你就会从原始页面移动到链接后面的页面。

在您的情况下,您有一个结果站点,其中包含一些指向亚马逊上产品的 URL,然后您单击此结果列表中的链接之一并被移动到详细信息站点。在这种情况下,站点发生了变化,而您想要在 for 循环中迭代的其余元素不存在——这就是您得到异常的原因。

为什么不使用搜索结果网站来提取标题和图像?两者都在那里,您只需更改 XPath 表达式即可获得 lien 的正确字段。

更新

要从搜索结果站点中获取标题,请提取要单击的 a 元素的 h2 元素中的文本。

要获取图像,您需要在 li 元素中获取另一个 div:在 XPath 中选择 div[2] 您需要选择 div[1] 以获取图像。

如果您在浏览器中打开搜索结果站点并使用开发人员工具查看源代码,您可以看到要为元素使用哪个 XPath 表达式。

【讨论】:

  • 请您解释一下如何使用搜索结果站点提取标题和图像? @GHajba
  • 请问,我应该使用哪个元素来提取标题和图像:self.driver.find_element_by_xpath('') 或lien.find_element_by_xpath()? @GHajba
  • 对于标题lien,因为它是链接。对于图像,您需要使用 result 与图像的 XPath 略有不同。
  • 拜托,我测试了:item['Title'] = result.find_element_by_xpath('//div[@class="s-item-container"]/div/div/div[2]/ div[1]/a/h2').text , item['Title'] = lien.find_element_by_xpath('//h2') 和 item['Title']=self.driver.find_element_by_xpath('//div[@ id="a-page"]/div/div[1]/div[1]/h1[@id="aiv-content-title"]').text ,但我没有得到任何结果。感谢您为我提供的所有帮助@GHajba
  • 您是否尝试致电lien.find_element_by_xpath('./h2'),因为您正在寻找一个元素并且在链接中只有一个可用的h2。我希望你也删除了lien.click() 命令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多