Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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 web抓取的性能改进?_Python_Selenium - Fatal编程技术网

Python web抓取的性能改进?

Python web抓取的性能改进?,python,selenium,Python,Selenium,作为个人项目的一部分,我尝试编写一个web scraper,它可以访问我的instagram帐户,在给定的对话中浏览所有DMs 在某种程度上,它运行得相当好;我的问题是,我正在尝试抓取的小组对话非常活跃,可以追溯到2017年(因此它有很多信息),在某个时候,ChromeeEngine只是滞后了太多,以至于整个过程超时并崩溃。 有什么方法可以提高性能吗?也许有一种完全不同的方式我应该这样做 def userlist(): #create my selenium instance o

作为个人项目的一部分,我尝试编写一个web scraper,它可以访问我的instagram帐户,在给定的对话中浏览所有DMs

在某种程度上,它运行得相当好;我的问题是,我正在尝试抓取的小组对话非常活跃,可以追溯到2017年(因此它有很多信息),在某个时候,ChromeeEngine只是滞后了太多,以至于整个过程超时并崩溃。
有什么方法可以提高性能吗?也许有一种完全不同的方式我应该这样做

def userlist():
    #create my selenium instance
    options = webdriver.ChromeOptions()
    #options.add_argument('headless')
    options.add_argument('window-size=1200x600')
    driver = webdriver.Chrome(chrome_options=options)

    #Log into instagram
    driver.get("https://instagram.com")
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "username")))
    driver.find_element_by_name("username").send_keys("###############")
    driver.find_element_by_name("password").send_keys("###############")
    driver.find_element_by_xpath("//button[contains(@class, 'sqdOP  L3NKy   y3zKF     ')]").click()
    sleep(5)
    driver.find_element_by_xpath("//button[contains(@class, 'sqdOP yWX7d    y3zKF     ')]").click()
    sleep(3)
    driver.find_element_by_xpath('//button[contains(@class, "aOOlW   HoLwm ")]').click()
    sleep(1)

    #go to the group convo
    driver.get("https://instagram.com/direct/inbox")
    sleep(3)
    driver.find_element_by_xpath(
        '//a[contains(@href, "/direct/t/###################")]').click()
    sleep(1)
    print("Verbindung zur Gruppe hergestellt!")
    print("")
    print("Beginne Auszählung, bitte etwas Geduld haben...")

    #scroll to the top to load older messages, until that isn't possible anymore - do this by checking for scrollTop > 0 every 3 seconds
    i = 1
    while int(driver.find_element_by_xpath('//div[contains(@class, "frMpI  -sxBV")]').get_attribute("scrollTop")) > 0:
        driver.execute_script("document.getElementsByClassName('frMpI  -sxBV')[0].scrollTop = 0")
        sleep(3)
        print(f"Schritt {i}...")
        print("")
        i += 1

    #add author names to a list
    for element in driver.find_elements_by_xpath("//div[contains(@class, '_7UhW9  PIoXz       MMzan   _0PwGv            fDxYl')]"):
        text = element.get_attribute("innerHTML")
        if not re.match(".*[0-9]:.*", text):
            userslist.append(element.get_attribute("innerHTML"))

    print("")

好的,如果您的问题在while循环中,请尝试将此代码与try和except一起使用

while int(driver.find_element_by_xpath('//div[contains(@class, "frMpI  -sxBV")]').get_attribute("scrollTop")) > 0:
    try:
        driver.execute_script("document.getElementsByClassName('frMpI  -sxBV')[0].scrollTop = 0")
        sleep(3)
        print(f"Schritt {i}...")
        print("")
        i += 1
    except Exception as e :
        print(e)
        i += 0
这段代码将打印错误并进行处理,如果出现错误,将自动重放循环,因此您不必担心,只需用while循环替换此循环即可
希望它对您有所帮助

您尝试过“尝试除外”方法吗?请您详细说明一下好吗?没问题,我将编辑代码并将其提供给您。但是我理解你因为超时而收到的错误。我不理解你的INSTAGRAM信息还有什么问题。你分析了你的代码吗?哪一部分慢?你知道你的代码在
循环中包含
睡眠(3)
吗?@MisterMiyagi我很抱歉使用ALLCAPSOh,现在我明白你的意思了!非常感谢,无论发生什么情况,这确实会有助于继续下去,我想我暂时会这样做。尽管如此,我还是想找到一种更有效的方法来做这件事。