【发布时间】:2020-05-08 06:24:01
【问题描述】:
我正在尝试从 iframe 中提取评论,我正在尝试切换到 iframe,我猜我成功切换了,但无法访问更多标签和属性..
我尝试了多种解决方案,即评论,我需要获得特定的评论 div,或者即使我们获得 iframe 的整页源代码,我也可以通过 Beautifulsoup 进行解析。
我的代码:
from telnetlib import EC
from time import sleep
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def get_pro_reviews():
driver = webdriver.Chrome()
driver.get(web_url)
try:
driver.execute_script("window.scrollTo(0, 200)")
driver.implicitly_wait(10)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.XPATH, '/html/body/div[5]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/ul/li[2]'))).click()
print('==> Review tab Clicked')
driver.implicitly_wait(5)
driver.execute_script("window.scrollTo(200, 1200)")
sleep(5)
except Exception as error:
product['Review'] = 'Reviews Not available'
print(f'Error in Clicking review tab ==>{error}')
try:
sleep(5)
# driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
# iframes=driver.find_elements_by_tag_name("iframe")
# chn=driver.switch_to.frame(iframes[0])
print(driver)
che = WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(
driver.find_element_by_xpath("//*[@id='product-evaluation']")))
print('==> Swithched to iframe')
print(che)
print(driver)
review_elem = driver.page_source()
# review_elem = driver.find_element_by_css_selector('div.feedback-list-wrap')
print(review_elem)
# review_elem = chn.find_element_by_xpath('/html/body/div/div[5]')
review_elem_source_code = review_elem.get_attribute("innerHTML")
review_elem_soup: BeautifulSoup = BeautifulSoup(review_elem_source_code, 'html.parser')
print(review_elem_soup)
for review_raw in review_elem_soup.find_elements_by_css_selector('div.feedback-item'):
print(review_raw)
driver.switch_to.default_content()
except Exception as error:
print(f'Error in getting Reviews ==>{error}')
错误:
Error in getting Reviews ==>'str' object is not callable
这些是我需要的评论!
【问题讨论】:
-
你能放一张你要检索哪些内容的图片吗?
-
@Armando 是的,让我说吧!
标签: python selenium-webdriver iframe web-scraping selenium-chromedriver