我正在尝试从kompass.com网站中获取公司信息。但由于每个公司简介都提供不同数量的细节信息,因此某些页面可能会有缺失元素。例如,并非所有公司都具有“协会”信息。在这种情况下,我的脚本会花费很长时间来搜索这些缺失的元素。有没有办法可以加速搜索过程?以下是我脚本的摘录:
import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementNotVisibleException
from lxml import html
def init_driver():
driver = webdriver.Firefox()
driver.wait = WebDriverWait(driver, 5)
return driver
def convert2text(webElement):
if webElement != []:
webElement = webElement[0].text.encode('utf8')
else:
webElement = ['NA']
return webElement
link='http://sg.kompass.com/c/mizkan-asia-pacific-pte-ltd/sg050477/'
driver = init_driver()
driver.get(link)
driver.implicitly_wait(10)
name = driver.find_elements_by_xpath("//*[@id='productDetailUpdateable']/div[1]/div[2]/div/h1")
name = convert2text(name)
## Problem:
associations = driver.find_elements_by_xpath("//body//div[@class='item minHeight']/div[@id='associations']/div/ul/li/strong")
associations = convert2text(associations)
每一页的抓取需要超过一分钟,我有超过26,000页需要抓取。
WebDriverWait
,但是为什么还要使用implicitly_wait()
? - Andersson