from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup
#browser = webdriver.Firefox()#Chrome('./chromedriver.exe')
YOUTUBER_HOME_PAGE_URL = "https://www.imdb.com/title/tt6452574/reviews?ref_=tt_ov_rt"
PATIENCE_TIME = 60
LOAD_MORE_BUTTON_XPATH = '//*[@id="load-more-trigger"]'
driver = webdriver.Chrome(r"C:\Users\Panu\Dropbox\Dr.Gokhan Sir\chromedriver.exe")
driver.get(YOUTUBER_HOME_PAGE_URL)
while True:
try:
loadMoreButton = driver.find_element_by_xpath('//*[@id="load-more-trigger"]')
time.sleep(2)
loadMoreButton.click()
time.sleep(5)
except Exception as e:
print e
break
print "Complete"
time.sleep(10)
#Selenium hands the page source to Beautiful Soup
soup_level1=BeautifulSoup(driver.page_source, 'html.parser')
movie_containers = soup_level1.find_all('div', class_ = 'review-container')
print(type(movie_containers))
print(len(movie_containers))
sample_data = movie_containers
#div = sample_data.find_all('div', class_ = 'text show-more__control')
#sample_data.text for user review and titile
reviews = []
user_titles = []
for div in sample_data:
title = div.div.a.text
user_titles.append(title)
#print(div.text)
review = div.find_all('div', class_ = 'text show-more__control').text
reviews.append(review)
运行上述代码时出现错误。 AttributeError:ResultSet对象没有属性“文本”。您可能正在将项目列表像单个项目一样对待。当您打算致电find()时,您是否致电过find_all()?
答案 0 :(得分:0)
您的行:
review = div.find_all('div', class_ = 'text show-more__control').text
引起了问题。 div.find_all
将以该类作为列表对象返回所有div。然后,您在该列表对象上调用.text
。 python如何知道您要查看的文本?用类似以下内容迭代结果:
divs = div.find_all('div', class_ = 'text show-more__control')
for div in divs:
#inside of this loop you will be accessing each review
print(div)
print("") #doing this to print a blank line to "separate" them for you
然后使用所需的内容。