【问题标题】:Selenium is really slow for me, is there something wrong with my code?Selenium 对我来说真的很慢,我的代码有问题吗?
【发布时间】:2019-02-26 19:39:53
【问题描述】:

我是网页抓取和 python 的新手。在此之前我已经完成了一个脚本,效果很好。我在这个中做的基本相同,但运行速度较慢。 这是我的代码:

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import selenium
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
import time

start = time.time()
opp = Options()
opp.add_argument('-headless')
browser = webdriver.Firefox(executable_path = "/Users/0581279/Desktop/L&S/Watchlist/geckodriver", options=opp)
browser.delete_all_cookies()
browser.get("https://www.bloomberg.com/quote/MSGFINA:LX")

c = browser.page_source
soup = BeautifulSoup(c, "html.parser")
all = soup.find_all("span", {"class": "fieldValue__2d582aa7"})
price = all[6].text
browser.quit()
print(price)
end = time.time()
print(end-start)

有时,加载单个页面最多需要 2 分钟。我也只是在网上抓取彭博社。 任何帮助将不胜感激:)

【问题讨论】:

  • 您是否尝试过对代码的计时部分进行计时,以查看哪些特定行使其如此缓慢?这将有助于我们查看您的网络是否存在问题,或者您在解析结果时是否存在问题。
  • 问题似乎出在这个浏览器上。get("bloomberg.com/quote/MSGFINA:LX")
  • 可能是您创建 webdriver 的方式。尝试使用 chrome,或者删除不需要的选项。

标签: python selenium web-scraping


【解决方案1】:

使用requestsBeautifulSoup,您可以轻松快速地抓取信息。这里的代码可以获取bloomberg的MSGFINA:LX关键统计数据

import requests
from bs4 import BeautifulSoup

headers = {
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/72.0.3626.119 Safari/537.36',
    'DNT': '1'
}

response = requests.get('https://www.bloomberg.com/quote/MSGFINA:LX', headers=headers)
page = BeautifulSoup(response.text, "html.parser")

key_statistics = page.select("div[class^='module keyStatistics'] div[class^='rowListItemWrap']")
for key_statistic in key_statistics:
    fieldLabel = key_statistic.select_one("span[class^='fieldLabel']")
    fieldValue = key_statistic.select_one("span[class^='fieldValue']")
    print("%s: %s" % (fieldLabel.text, fieldValue.text))

【讨论】:

  • 我开始收到“你是机器人页面吗”你知道我如何绕过这个而不必使用 selenium 吗?
【解决方案2】:

Selenium 影响一些参数,例如:

If the site is slow, the Selenium script is slow.

If the performance of the internet connection is not good, the Selenium script is slow.

If the computer running the script is not performing well, the Selenium script is slow.

这些情况通常不在我们手中。但是编程是。 提高速度的一种方法是阻止图像加载(如果我们不使用它。) 阻止加载图像会影响运行时。这是阻止它的方法:

opp.add_argument('--blink-settings=imagesEnabled=false')

当您打开驱动程序时,您不需要再次使用BeautifulSoap 函数来获取数据。 Selenium 函数提供了它。尝试下面的代码,Selenium 会更快

from selenium import webdriver

from selenium.webdriver.firefox.options import Options
import time

start = time.time()
opp = Options()
opp.add_argument('--blink-settings=imagesEnabled=false')

driver_path = r'Your driver path'
browser = webdriver.Chrome(executable_path=driver_path , options=opp)

browser.delete_all_cookies()
browser.get("https://www.bloomberg.com/quote/MSGFINA:LX")

get_element = browser.find_elements_by_css_selector("span[class='fieldValue__2d582aa7']")


print(get_element[6].text)
browser.quit()

end = time.time()
print(end-start)

【讨论】:

    【解决方案3】:

    所以我对您的代码进行了一些修改,几乎可以立即加载它,我使用了我已经安装的 chrome 驱动程序,然后运行了以下代码。

    import requests
    from bs4 import BeautifulSoup
    from selenium import webdriver
    import selenium
    import time
    
    start = time.time()
    browser = webdriver.Chrome("/Users/XXXXXXXX/Desktop/Programming/FacebookControl/package/chromedriver")
    browser.get("https://www.bloomberg.com/quote/MSGFINA:LX")
    
    c = browser.page_source
    soup = BeautifulSoup(c, "html.parser")
    all = soup.find_all("span", {"class": "fieldValue__2d582aa7"})
    price = all[6].text
    browser.quit()
    print(price)
    end = time.time()
    print(end-start)
    

    在测试时他们确实阻止了我哈哈,可能想每隔一段时间更改一次标题。它还打印了价格。

    chromedriver链接http://chromedriver.chromium.org/

    希望这会有所帮助。

    输出是这样的:

    34.54
    7.527994871139526
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多