Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在尝试使用selenium的通用爬行器模板来爬行scrapy爬行器,但是当爬行到产品详细信息页面时,它给出了错误的数据_Python_Selenium_Scrapy_Scrapy Selenium - Fatal编程技术网

Python 我正在尝试使用selenium的通用爬行器模板来爬行scrapy爬行器,但是当爬行到产品详细信息页面时,它给出了错误的数据

Python 我正在尝试使用selenium的通用爬行器模板来爬行scrapy爬行器,但是当爬行到产品详细信息页面时,它给出了错误的数据,python,selenium,scrapy,scrapy-selenium,Python,Selenium,Scrapy,Scrapy Selenium,大宗报价 我试图提取href链接,然后加入url,然后用加入的url给出seleniumRequest 我的代码可以工作,它会对数据进行爬网,但结果会产生相同的重复数据 爬网的数据将是重复的 看起来一切正常,没有错误,但输出是重复的,数据也来自不同的产品链接 ############# STACK OVERFLOW PLAESE HELP I'm AN BEGGINER IN SCRAPY WITH SELENIUM ########### ############# I THINK SOMET

大宗报价

我试图提取href链接,然后加入url,然后用加入的url给出seleniumRequest 我的代码可以工作,它会对数据进行爬网,但结果会产生相同的重复数据 爬网的数据将是重复的

看起来一切正常,没有错误,但输出是重复的,数据也来自不同的产品链接

############# STACK OVERFLOW PLAESE HELP I'm AN BEGGINER IN SCRAPY WITH SELENIUM ###########
############# I THINK SOMETHING WITH MY PRODUCT PRICE URL #############
############# SOMETHING WRONG WITH URL #############################
#########This is my code

import scrapy
from scrapy.selector import Selector
from scrapy_selenium import SeleniumRequest
from selenium.webdriver.common.keys import Keys
from time import sleep

class AmazonSpider(scrapy.Spider):
    name = 'Amazon'


    def start_requests(self):
        yield SeleniumRequest(
            url='https://www.amazon.com',
            wait_time=3,
            screenshot=True,
            callback=self.parse
        )

    def parse(self, response):
        driver = response.meta['driver']
        search_input = 
        driver.find_element_by_xpath("//input[@id='twotabsearchtextbox']")
        search_input.send_keys('smartphones')
        search_input.send_keys(Keys.ENTER)

        html = driver.page_source
        response_obj = Selector(text=html)
        driver.set_window_size(1920, 1080)

        links = response_obj.xpath("//h2[@class='a-size-mini a-spacing-none a-color-base s-line-clamp-2']/a")
        for link in links:

            product_link = link.xpath(".//@href").get()

            absolute_url = response.urljoin(product_link)

            yield SeleniumRequest(url=absolute_url, wait_time=8, callback=self.parse_price, 
                                                        dont_filter=True )

     def parse_price(self, response):

        driver = response.meta['driver']
        htmlbody = driver.page_source

        response_object = Selector(text=htmlbody)

        driver.set_window_size(1920, 1080)

        sleep(5)
        name = response_object.xpath("//h1[@class='a-size-large a-spacing-none']/span").get()

        yield {
            'name': name
        }

也许可以使用
print()
来查看您在
绝对url
中得到了什么-也许它需要更多的东西。并检查
reponse.url
中的内容。您使用的是
don_fitler=True
,因此它可能会多次加载相同的url(如果您在
请求中使用它的话)。您还应该在web浏览器中检查它是如何工作的。可能它不使用request
GET
,而是使用
POST
,您需要在
selenium
中单击
链接。简而言之:您必须调试数据。如果您使用
驱动程序.page\u source
,那么使用
驱动程序是没有意义的。设置窗口大小(…)
-它是无用的。它无法更改已作为字符串的HTML。是否在不使用
headless
查看浏览器的情况下对其进行测试?似乎所有的请求都使用相同的窗口/选项卡,因为代码运行速度很快,所以浏览器没有时间加载新的url,而且您总是得到相同的HTML。在获取驱动程序之前,您必须
睡眠
。page\u source
。但即使这样有时也可能不起作用。@furas感谢您的帮助。我希望您的建议能帮助我解决问题。是的,响应url有问题。我正在尝试调试。