亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python Beatifulsoup 無法正確使用 selenium 的結果

Python Beatifulsoup 無法正確使用 selenium 的結果

守候你守候我 2022-06-07 19:54:49
我正在嘗試使用 beautifulsoup 解析網頁。我可以看到頁面已使用 chromedriver 在 selenium 中正確加載,但最終結果為 null,當我在 beautifulsoup 中看到解析的頁面打印時,它沒有顯示 selenium 在其自動瀏覽器中顯示的整個頁面。我為此目的使用的代碼是:page_soup = soup(driver.page_source, "html.parser")print (page_soup)containers = page_soup.findAll("div", class_="row ploc-l-row--gutterV flex-wrap flex-align-start flex-center-vertical")print (len(containers))我需要訪問每個合作伙伴信息,但結果為空。我正在處理的頁面是https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619
查看完整描述

3 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

結果是使用 javascript 加載的。您需要等到搜索結果加載完畢后再進行抓取。這是一個工作示例,


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()

結果是5


查看完整回答
反對 回復 2022-06-07
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

僅供參考,該頁面使用 jQuery,這使得這很容易:

driver.execute_script("return $('div[class=\"row ploc-l-row--gutterV flex-wrap flex-align-start flex-center-vertical\"]').length")


查看完整回答
反對 回復 2022-06-07
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

根據您的評論澄清,我檢索了搜索結果中顯示的每個合作伙伴的合作伙伴名稱:

使用 BeautifulSoup 語法:

partnerWebElements = page_soup.findAll(title="View Profile")

僅使用 Selenium 語法:

partnerWebElements = driver.find_elements_by_xpath("//a[@title='View Profile']")

然后,您可以獲得每個合作伙伴名稱的文本,如下所示:

for partnerWebElement in partnerWebElements:
    print(partnerWebElement.text);


查看完整回答
反對 回復 2022-06-07
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號