我希望在 Crunchbase 上尽可能多地收集有关电动汽车公司的数据,从名称、行业和总部位置开始。除了每次,我都尝试遇到一个问题:首先,我遇到了 NoSuchElementException,所以我添加了一些 WebDriverWait 代码,现在我遇到了“NameError: name 'By' is not defined”。我什至如何定义?任何帮助表示赞赏! :)
###Import Selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
#set driver path
PATH = "C:/Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.crunchbase.com/search/organizations/field/organization.companies/categories/electric-vehicle")
print(driver.title)
#wait to find element
WebDriverWait(driver, 20).until(
EC.visibility_of_element_located(
(By.XPATH, ('/html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[2]/div/field-formatter/identifier-formatter/a/div/div')
)))
companies = driver.find_elements_by_class_name('ng-star-inserted')
#iterate through companies for data
for company in companies:
name = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[2]/div/field-formatter/identifier-formatter/a/div/div').text
industry = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[3]/div/field-formatter/identifier-multi-formatter').text
hq = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[4]/div/field-formatter/identifier-multi-formatter/span').text
print(name,industry,hq)