Python单击'更多'按钮不起作用

问题描述 投票:0回答:1

我尝试为每个评论单击“更多”按钮,以便可以将这些文本评论扩展为完整内容,然后尝试刮取这些文本评论。没有点击“更多”按钮,我最终得到的是“这个房间很干净。位置很好。”

我尝试了几种不同的功能来解决它​​,例如硒按钮单击和ActionChain,但我想我没有正确使用它们。有人可以帮我解决这个问题吗?

下面是我当前的代码:我没有上载整个代码来避免一些不必要的输出(试图使其变得简单)。

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains

#Incognito Mode
option=webdriver.ChromeOptions()
option.add_argument("--incognito")

#Open Chrome
driver=webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",chrome_options=option)

#url I want to visit.
lists=['https://www.tripadvisor.com/VacationRentalReview-g30196-d6386734-Hot_51st_St_Walk_to_Mueller_2BDR_Modern_sleeps_7-Austin_Texas.html']

for k in lists:

    driver.get(k)
    html =driver.page_source
    soup=BeautifulSoup(html,"html.parser")
    time.sleep(3)
    listing=soup.find_all("div", class_="review-container")

    for i in range(len(listing)):

        try:
            #First, I tried this but didn't work.
            #link = driver.find_element_by_link_text('More')
            #driver.execute_script("arguments[0].click();", link)

            #Second, I tried ActionaChains but didn't work.
            ActionChains(driver).move_to_element(i).click().perform()
        except:
            pass

        text_review=soup.find_all("div", class_="prw_rup prw_reviews_text_summary_hsx")
        text_review_inside=text_review[i].find("p", class_="partial_entry")
        review_text=text_review_inside.text

        print (review_text)
python selenium button click
1个回答
0
投票

您在所有此代码中最大的错误是except: pass.如果没有此错误,您将很久以前就可以解决问题。代码引发错误消息,其中包含所有信息,但您看不到它。您至少可以使用

except Exception as ex:
    print(ex)

问题是move_to_element()不适用于BeautifulSoup元素。我必须是Selenium的元素-如

link = driver.find_element_by_link_text('More')

ActionChains(driver).move_to_element(link)

但是在执行了一些功能之后,Selenium需要一些时间来完成它-并且Python必须等待唤醒。

我不使用BeautifulSoup来获取数据,但是如果您要使用它,则在单击所有链接后将获取driver.page_source。否则,您每次单击后都必须一次又一次地获得driver.page_source

有时单击之后,您甚至必须再次获取Selenium元素-因此,我首先获得输入以单击More,然后再获得partial_entry以获得评论。

[我发现在第一条评论中单击More会显示所有评论的文本,因此无需单击所有More

使用Firefox 69,Linux Mint 19.2,Python 3.7.5,Selenium 3.141测试


#from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
import time

#Incognito Mode
option = webdriver.ChromeOptions()
option.add_argument("--incognito")

#Open Chrome
#driver = webdriver.Chrome(executable_path="C:/Users/chromedriver.exe",chrome_options=option)

driver = webdriver.Firefox()

#url I want to visit.
lists = ['https://www.tripadvisor.com/VacationRentalReview-g30196-d6386734-Hot_51st_St_Walk_to_Mueller_2BDR_Modern_sleeps_7-Austin_Texas.html']

for url in lists:

    driver.get(url)
    time.sleep(3)

    link = driver.find_element_by_link_text('More')

    try:
        ActionChains(driver).move_to_element(link)
        time.sleep(1) # time to move to link

        link.click()
        time.sleep(1) # time to update HTML
    except Exception as ex:
        print(ex)

    description = driver.find_element_by_class_name('vr-overview-Overview__propertyDescription--1lhgd')
    print('--- description ---')
    print(description.text)
    print('--- end ---')

    # first "More" shows text in all reviews - there is no need to search other "More"
    first_entry = driver.find_element_by_class_name('entry')
    more = first_entry.find_element_by_tag_name('span')

    try:
        ActionChains(driver).move_to_element(more)
        time.sleep(1) # time to move to link

        more.click()
        time.sleep(1) # time to update HTML
    except Exception as ex:
        print(ex)

    all_reviews = driver.find_elements_by_class_name('partial_entry')
    print('all_reviews:', len(all_reviews))

    for i, review in enumerate(all_reviews, 1):
        print('--- review', i, '---')
        print(review.text)
        print('--- end ---')

编辑:

要跳过响应,我搜索所有class="wrap",然后在每个包装中搜索class="partial_entry"。我的每篇评论只能是一篇评论,而最终是一篇回应。评论具有阿尔萨斯索引[0]。一些包装不进行审查,因此它们将提供空白列表-在从列表中获取元素[0]之前,我必须进行检查。

all_reviews = driver.find_elements_by_class_name('wrap')
#print('all_reviews:', len(all_reviews))

for review in all_reviews:
    all_entries = review.find_elements_by_class_name('partial_entry')
    if all_entries:
        print('--- review ---')
        print(all_entries[0].text)
        print('--- end ---')
© www.soinside.com 2019 - 2024. All rights reserved.