【发布时间】:2016-06-15 01:57:15
【问题描述】:
我正在尝试从 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()...为什么?
标签: python selenium selenium-webdriver web-scraping