【问题标题】:ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item.ResultSet 对象没有属性“文本”。您可能将项目列表视为单个项目。
【发布时间】:2019-08-07 10:02:24
【问题描述】:
        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() 吗?

【问题讨论】:

标签: python pandas web-scraping beautifulsoup imdb


【解决方案1】:

您的线路:

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

然后使用你想要的。

【讨论】:

  • 它在这段代码中给出了同样的错误 divs = div.find_all('div', class_ = 'text show-more__control').text
  • 删除.text
  • @Coder 我的错,你不需要.text,请参阅编辑后的解决方案
  • 通过使用它,它将所有评论组合在一起,但我希望它分开.......divs = soup_level1.find_all('div', class_ = 'text show-more__control') data = [ ] 对于 div 中的 div:review_data = div.text data.append(review_data)
  • @Coder 对不起,你能澄清一下吗?
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2021-11-09
  • 2015-10-05
  • 2021-03-08
  • 2015-09-16
  • 1970-01-01
相关资源
最近更新 更多