Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 HTML字符串传递给Scrapy,以将URL添加到要刮取的URL的Scrapy列表中_Python_Selenium_Web Scraping_Scrapy_Scrapy Spider - Fatal编程技术网

Python 将Selenium HTML字符串传递给Scrapy,以将URL添加到要刮取的URL的Scrapy列表中

Python 将Selenium HTML字符串传递给Scrapy,以将URL添加到要刮取的URL的Scrapy列表中,python,selenium,web-scraping,scrapy,scrapy-spider,Python,Selenium,Web Scraping,Scrapy,Scrapy Spider,我对Python、Scrapy和Selenium非常陌生。因此,如果您能提供任何帮助,我们将不胜感激 我希望能够将从Selenium获得的HTML作为页面源代码,并将其处理为一个Scrapy响应对象。主要原因是能够将SeleniumWebDriver页面源中的URL添加到Scrapy将解析的URL列表中 再次感谢您的帮助 作为第二个问题,是否有人知道如何查看Scrapy查找和刮取的URL列表中的URL列表 谢谢 *******编辑******* 下面是我正在尝试做的一个例子。我搞不懂第五部分 c

我对Python、Scrapy和Selenium非常陌生。因此,如果您能提供任何帮助,我们将不胜感激

我希望能够将从Selenium获得的HTML作为页面源代码,并将其处理为一个Scrapy响应对象。主要原因是能够将SeleniumWebDriver页面源中的URL添加到Scrapy将解析的URL列表中

再次感谢您的帮助

作为第二个问题,是否有人知道如何查看Scrapy查找和刮取的URL列表中的URL列表

谢谢

*******编辑******* 下面是我正在尝试做的一个例子。我搞不懂第五部分

class AB_Spider(CrawlSpider):
    name = "ab_spider"
    allowed_domains = ["abcdef.com"]
    #start_urls = ["https://www.kickstarter.com/projects/597507018/pebble-e-paper-watch-for-iphone-and-android"
    #, "https://www.kickstarter.com/projects/801465716/03-leagues-under-the-sea-the-seaquestor-flyer-subm"]
    start_urls = ["https://www.abcdef.com/page/12345"]

    def parse_abcs(self, response):
        sel = Selector(response)
        URL = response.url

        #part 1: check if a certain element is on the webpage
        last_chk = sel.xpath('//ul/li[@last_page="true"]')
        a_len = len(last_chk)

        #Part 2: if not, then get page via selenium webdriver
        if a_len == 0:
            #OPEN WEBDRIVER AND GET PAGE
            driver = webdriver.Firefox()
            driver.get(response.url)    

        #Part 3: run script to ineract with page until certain element appears
        while a_len == 0:
            print "ELEMENT NOT FOUND, USING SELENIUM TO GET THE WHOLE PAGE"

            #scroll down one time
            driver.execute_script("window.scrollTo(0, 1000000000);")

            #get page source and check if last page is there
            selen_html = driver.page_source
            hxs = Selector(text=selen_html)
            last_chk = hxs.xpath('//ul/li[@last_page="true"]')

            a_len = len(last_chk)

        driver.close()

        #Part 4: extract the URLs in the selenium webdriver URL 
        all_URLS = hxs.xpath('a/@href').extract()

        #Part 5: all_URLS add to the Scrapy URLs to be scraped
仅从方法中提取实例并提供回调:

class AB_Spider(CrawlSpider):
    ...

    def parse_abcs(self, response):
        ...

        all_URLS = hxs.xpath('a/@href').extract()

        for url in all_URLS:
            yield Request(url, callback=self.parse_page)

    def parse_page(self, response):
        # Do the parsing here

到目前为止你都试了些什么?我还没找到任何可以尝试的方法。我不知道如何访问Scrapy的URL队列。我知道如何从HTML中提取URL。所以我想简单的问题是如何手动将URL添加到Scrapy队列中。