为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号