3 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
結(jié)果是使用 javascript 加載的。您需要等到搜索結(jié)果加載完畢后再進(jìn)行抓取。這是一個(gè)工作示例,
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup as soup
import time
url = 'https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do'
driver = webdriver.Chrome(executable_path='C:/Selenium/chromedriver.exe')
driver.get(url)
SearchString = 'CALIFORNIA'
Location = driver.find_element_by_name("location")
Location.send_keys(SearchString)
#search = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located(By.XPATH,"//li//span[contains(text(),'"+SearchString+"')]"))
#search.click()
time.sleep(3)
driver.find_element_by_xpath("//li//span[contains(text(),'"+SearchString+"')]").click()
driver.find_element_by_id("searchBtn").click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,'searchResultsList')))
time.sleep(3)
page_soup = soup(driver.page_source, "html.parser")
print(page_soup.prettify())
containers = page_soup.findAll("div", class_="row ploc-l-row--gutterV flex-wrap flex-align-start flex-center-vertical")
print (len(containers))
driver.close()
結(jié)果是5

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
僅供參考,該頁(yè)面使用 jQuery,這使得這很容易:
driver.execute_script("return $('div[class=\"row ploc-l-row--gutterV flex-wrap flex-align-start flex-center-vertical\"]').length")

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
根據(jù)您的評(píng)論澄清,我檢索了搜索結(jié)果中顯示的每個(gè)合作伙伴的合作伙伴名稱(chēng):
使用 BeautifulSoup 語(yǔ)法:
partnerWebElements = page_soup.findAll(title="View Profile")
僅使用 Selenium 語(yǔ)法:
partnerWebElements = driver.find_elements_by_xpath("//a[@title='View Profile']")
然后,您可以獲得每個(gè)合作伙伴名稱(chēng)的文本,如下所示:
for partnerWebElement in partnerWebElements: print(partnerWebElement.text);
添加回答
舉報(bào)