本文介绍了Selenium 速度慢,还是我的代码错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试使用 Python 登录 Quora,然后抓取一些东西.

So I'm trying to login to Quora using Python and then scrape some stuff.

我正在使用 Selenium 登录该站点.这是我的代码:

I'm using Selenium to login to the site. Here's my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get('http://www.quora.com/')

username = driver.find_element_by_name('email')
password = driver.find_element_by_name('password')

username.send_keys('email')
password.send_keys('password')
password.send_keys(Keys.RETURN)

driver.close()

现在的问题:

  1. 查找和填写登录表单花了大约 4 分钟,速度非常慢.我可以做些什么来加快进程?

  1. It took ~4 minutes to find and fill the login form, which painfully slow. Is there something I can do to speed up the process?

当它登录时,我如何确保没有错误?换句话说,我如何检查响应代码?

When it did login, how do I make sure there were no errors? In other words, how do I check the response code?

如何使用 selenium 保存 cookie,以便在登录后继续抓取?

How do I save cookies with selenium so I can continue scraping once I login?

如果没有办法让selenium更快,还有其他的登录方式吗?(Quora 没有 API)

If there is no way to make selenium faster, is there any other alternative for logging in? (Quora doesn't have an API)

推荐答案

我遇到了类似的问题,使用 ChromeDriver 在 Python selenium 中调用 find_elements_xxx 非常慢.我最终将问题追溯到我在 find_element_xxx() 调用之前所做的 driver.implicitly_wait() 调用;当我拿出来时,我的 find_element_xxx() 调用跑得很快.

I had a similar problem with very slow find_elements_xxx calls in Python selenium using the ChromeDriver. I eventually tracked down the trouble to a driver.implicitly_wait() call I made prior to my find_element_xxx() calls; when I took it out, my find_element_xxx() calls ran quickly.

现在,我知道这些元素在我调用 find_elements_xxx() 时就在那里.所以我无法想象为什么implicit_wait会影响这些操作的速度,但它确实影响了.

Now, I know those elements were there when I did the find_elements_xxx() calls. So I cannot imagine why the implicit_wait should have affected the speed of those operations, but it did.

这篇关于Selenium 速度慢,还是我的代码错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:35