【发布时间】: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