2016-10-11 20 views
1

tripadvisorからユーザーコメントを翻訳しようとしています。
マイコード: -セレンが要素の繰り返し処理に失敗しました

1.](言語ドロップダウンから)のみポルトガルコメントを選択し、

2]次にコメントのそれぞれを拡大、

3]は、すべてのこれらの拡張のコメントを保存しますリスト

4]に続いて、画面上で英語&プリントに変換し

from selenium import webdriver 
from selenium.webdriver.common.by import By 
import time 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
com_=[] # To save translated comments 
expanded_comments=[] # To save expanded comments 
driver = webdriver.Chrome("C:\Users\shalini\Downloads\chromedriver_win32\chromedriver.exe") 
driver.maximize_window() 

def expand_reviews(driver): 
    # TRYING TO EXPAND REVIEWS (& CLOSE A POPUP)  
    try: 
     driver.find_element_by_class_name("moreLink").click() 
    except: 
     print "err" 
    try: 
     driver.find_element_by_class_name("ui_close_x").click() 
    except: 
     print "err" 
    try: 
     driver.find_element_by_class_name("moreLink").click() 
    except: 
     print "err3" 




def save_comments(driver): 
    expand_reviews(driver) 
    # SELECTING ALL EXPANDED COMMENTS 
    expanded_com_elements=driver.find_elements_by_class_name("entry") 
    time.sleep(3) 
    for i in expanded_com_elements: 
     expanded_comments.append(i.text) 


    # SELECTING ALL GOOGLE-TRANSLATOR links 
    gt= driver.find_elements(By.CSS_SELECTOR,".googleTranslation>.link") 

    # NOW PRINTING TRANSLATED COMMENTS 
    for i in gt: 
     try: 
      driver.execute_script("arguments[0].click()",i) 
      #i.click().perform() 
      com=driver.find_element_by_class_name("ui_overlay").text 
      com_.append(com) 
      time.sleep(5) 
      driver.find_element_by_class_name("ui_close_x").click().perform() 
      time.sleep(5) 
     except Exception as e: 
      pass 
      #print e 



for i in range(282): 
    page=i*10 
    url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or"+str(page)+"-TAP-Portugal#REVIEWS" 
    driver.get(url) 
    wait = WebDriverWait(driver, 10) 
    if i==0: 
     # SELECTING PORTUGUESE COMMENTS ONLY # Run for one time then iterate over pages 
     try: 
      langselction = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.sprite-date_picker-triangle"))) 
      langselction.click() 
      driver.find_element_by_xpath("//div[@class='languageList']//li[normalize-space(.)='Portuguese first']").click() 
      time.sleep(5) 
     except Exception as e: 
      print e 

    save_comments(driver) 

=================

expanded_commentsは空のリストを返します。一部のコメントは保存され、一部はスキップされます。 最初のページは正しく保存されます(すべてのコメントが展開されます)。その後、最初のコメントのみが展開されずに保存されます。しかし、すべてのページの翻訳されたコメントがcom_

答えて

1

に変更されました。

from selenium import webdriver 
from selenium.webdriver.common.by import By 
import time 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Chrome("./chromedriver.exe") 
driver.maximize_window() 
url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-TAP-Portugal#REVIEWS" 
driver.get(url) 
wait = WebDriverWait(driver, 10) 



# SELECTING PORTUGUESE COMMENTS ONLY 
#show_lan = driver.find_element_by_xpath("//div[@class='languageList']/ul/li[contains(text(),'Portuguese first')]") 

try: 
    langselction = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.sprite-date_picker-triangle"))) 
    langselction.click() 
    driver.find_element_by_xpath("//div[@class='languageList']//li[normalize-space(.)='Portuguese first']").click() 
    time.sleep(5) 
except Exception as e: 
    print e 


# TRYING TO EXPAND REVIEWS (& CLOSE A POPUP)  
try: 
    driver.find_element_by_class_name("moreLink").click() 
except: 
    print "err" 
try: 
    driver.find_element_by_class_name("ui_close_x").click() 
except: 
    print "err" 
try: 
    driver.find_element_by_class_name("moreLink").click() 
except: 
    print "err3" 

# SELECTING ALL EXPANDED COMMENTS 
expanded_com_elements=driver.find_elements_by_class_name("entry") 
expanded_comments=[] 
time.sleep(3) 
for i in expanded_com_elements: 
    expanded_comments.append(i.text) 

# SELECTING ALL GOOGLE-TRANSLATOR links 
gt= driver.find_elements(By.CSS_SELECTOR,".googleTranslation>.link") 

# NOW PRINTING TRANSLATED COMMENTS 
for i in gt: 
    try: 
     driver.execute_script("arguments[0].click()",i) 
     #i.click().perform() 
     print driver.find_element_by_class_name("ui_overlay").text 
     time.sleep(5) 
     driver.find_element_by_class_name("ui_close_x").click().perform() 
     time.sleep(5) 
    except Exception as e: 
     pass 
     #print e 
+0

あなたは私http://stackoverflow.com/questions/40013400/selenium-python-webscrape-fails-after-first-iterationを解決する助けてくださいすることができます – shalini

+0

はい私はあなたをチェックし、更新します – Piyush

関連する問題