Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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

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 Scrapy爬行在chrome中使用带selenium的xpath元素选择花费了太多时间 问题:_Python_Selenium_Selenium Webdriver_Xpath_Scrapy - Fatal编程技术网

Python Scrapy爬行在chrome中使用带selenium的xpath元素选择花费了太多时间 问题:

Python Scrapy爬行在chrome中使用带selenium的xpath元素选择花费了太多时间 问题:,python,selenium,selenium-webdriver,xpath,scrapy,Python,Selenium,Selenium Webdriver,Xpath,Scrapy,我的问题是,我写了几个selenium Scrapy Webspider只是为了学校的任务,我想有礼貌地爬行(每页下载延迟=5*),但我甚至不必这样做,因为爬行一页要花太多时间为了在一个页面中找到所有元素,我甚至等待了30秒,在每个页面中,我都在寻找13个元素,它们可能出现在页面上,也可能不出现在页面上 我发现的问题是在通过xpath选择数据元素的过程中运行python脚本的Pycharm IDE终端和web bot spider selenium浏览器之间的问题 行为: 我的蜘蛛到底在做什么:

我的问题是,我写了几个selenium Scrapy Webspider只是为了学校的任务,我想有礼貌地爬行(每页下载延迟=5*),但我甚至不必这样做,因为爬行一页要花太多时间为了在一个页面中找到所有元素,我甚至等待了30秒,在每个页面中,我都在寻找13个元素,它们可能出现在页面上,也可能不出现在页面上

我发现的问题是在通过xpath选择数据元素的过程中运行python脚本的Pycharm IDE终端和web bot spider selenium浏览器之间的问题

行为: 我的蜘蛛到底在做什么:

  • 将500个URL从.txt文件加载到字典
  • 一个接一个地进行URL
  • 在每个URL上检查13个元素
  • 如果元素存在-收集数据,如果不存在,则设置为默认值
  • 最后,将收集的数据写入short.csv
  • 终端将在selenium chrome浏览器上使用POST方法要求通过xpath查找特定的一个元素,如果该元素在网页上不存在,selenium chrome浏览器将始终响应,每个xpath元素搜索延迟5秒

    selenium浏览器中的页面将在一秒钟内快速加载

    文档: 若未找到元素,则会发生异常,我在spider的代码中是这样处理的(IDE在selenium chrome上等待5秒钟以抛出异常):

    正在加载要爬网的URL[*已更新]:

    def start_requests(self):
    
        temp_dictionary = []
        # OPEN FILE AND LOAD URLS HERE
        with open("products_urls_en.txt") as file:
            for line in file:
                temp_dictionary.append({'url': line})
    
        # REMOVE DUPLICATES - if any  https://stackoverflow.com/questions/8749158/removing-duplicates-from-dictionary
    
        products_url_links = []
    
        for value in temp_dictionary:
            if value not in products_url_links:
                products_url_links.append({'url': value.get('url')})
    
        print("NUM OF LINKS: " + str(len(products_url_links)))
        self.counter_all = int(len(products_url_links))
    
        for url in products_url_links:
            yield scrapy.Request(url=url.get('url'), callback=self.parse)
    
    在爬行过程中,我对终端输出进行了如下描述:

    我甚至想在这里给出一些类似问题的链接,但我没有找到任何链接。人们主要在服务器端讨论问题,但我认为问题在我这边

    设置和版本
    • Python-3.6,PIP18.0
    • Pycharm-2018.1.5
    • Selenium-3.14.0(我认为是最新的,是通过Pycharm IDE下载的)
    • Scrapy-1.5.1(我认为是最新的,是通过Pycharm IDE下载的)
    • Windows-Win10 Pro 2018
    • SpiderSettings-所有默认设置(我尝试过-未更改问题)
    问:
    有人能给我解释一下,为什么要花这么多时间,以及如何修复它-请减少显式时间延迟?

    您可能使用了5秒的隐式等待。因此,当find_元素没有找到任何东西时,它会等待5秒钟,让它有机会出现…

    您能发布实际的代码吗?似乎您正在下载的每个xpath新页面和
    下载\u DELAY
    都启动了。您能给我们一个您想要爬网的urll和期望的项目吗。您应该阅读此内容,这可能对you@Granitosaurus由于我的版权,我不能在这里发布完整的代码,其他同学可以复制它,但是我在这里贴的所有特别的东西。。。在代码中,我创建了一个spider类,set name,之后我加载了我想要遍历的URL,我可以更新它。。。在那之后,我寻找我在这里写的元素。。。我的代码类似于:@Marek,你发布了很多代码,但我看不出你在哪里初始化
    驱动程序。另外,您的代码做了许多似乎与问题无关的事情,因此我建议您创建这个小程序,以便更容易地调查问题。
    
    def start_requests(self):
    
        temp_dictionary = []
        # OPEN FILE AND LOAD URLS HERE
        with open("products_urls_en.txt") as file:
            for line in file:
                temp_dictionary.append({'url': line})
    
        # REMOVE DUPLICATES - if any  https://stackoverflow.com/questions/8749158/removing-duplicates-from-dictionary
    
        products_url_links = []
    
        for value in temp_dictionary:
            if value not in products_url_links:
                products_url_links.append({'url': value.get('url')})
    
        print("NUM OF LINKS: " + str(len(products_url_links)))
        self.counter_all = int(len(products_url_links))
    
        for url in products_url_links:
            yield scrapy.Request(url=url.get('url'), callback=self.parse)