【问题标题】:My script fails to parse items from a complicated webpage我的脚本无法解析复杂网页中的项目
【发布时间】:2018-08-24 10:06:41
【问题描述】:

我使用 selenium 在 python 中编写了一个脚本,以从网页上抓取连接到每个 item name 的不同审阅者。单击see more 按钮时,很少有项目会显示很多评论者,很少有没有评论者。

我尝试以这样一种方式编写脚本,以便它可以从登录页面获取所有项目链接,然后遍历每个链接,然后单击 review tab 然后单击 see more 按钮并最后收集审阅者并重复相同的操作,直到没有更多的项目。

这里的主要问题是当脚本点击see more 按钮时,它会打开一个包含审阅者的新标签。

Link to the landing page

Link to one of such item containing reviews

Link to the page containing full reviews

这是我目前的尝试:

from urllib.parse import urljoin
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "https://eatstreet.com/madison-wi/restaurants"

def get_information(driver,link):
    driver.get(link)
    #collecting all the links connected to item names
    itemlinks = [urljoin(url,item.get_attribute("href")) for item in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"a.restaurant-header")))]
    for itemlink in itemlinks:
        driver.get(itemlink)

        #check whether there is any review
        revitem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"label[for='reviews']")))

        if revitem and (revitem.text != "Reviews (0)"):
            current = driver.current_window_handle
            wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"label[for='reviews']"))).click()
            wait.until(EC.visibility_of_element_located((By.LINK_TEXT,'See More Reviews'))).click()
            wait.until(EC.new_window_is_opened)
            driver.switch_to.window([window for window in driver.window_handles if window != current][0])
            while True:
                for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,'ul.reviews div.review .review-sidebar #dropdown_user-name'))):
                    print(item.text)

                try:
                    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".pagination-block a.next"))).click()
                    wait.until(EC.staleness_of(item))
                except Exception:break
            driver.switch_to.default_content()

if __name__ == '__main__':
    options = Options()
    options.add_argument("--disable-notifications")
    driver = webdriver.Chrome(chrome_options=options)
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,url)
    finally:  
        driver.quit()

我上面的脚本可以从包含评论的第一个可用项目中收集reviewers 名称,但是当它应该去下一个项目收集评论者姓名时它会抛出timeout exception 错误。这可能是因为当脚本switch to default content 并尝试重复该操作时,新打开的选项卡被取消选中。

下图显示了如何显示查看更多按钮:

【问题讨论】:

  • 这很简单,但是为什么不从“查看更多”按钮中提取链接并稍后打开它们呢?无需跟踪新标签。

标签: python python-3.x selenium selenium-webdriver web-scraping


【解决方案1】:

如果您需要关闭新窗口并返回初始窗口,请尝试替换

driver.switch_to.default_content()

driver.close()
driver.switch_to.window(current)

【讨论】:

  • @Topto,你最好也把revitem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"label[for='reviews']")))放到try/except或者把presence_of_element_located换成presence_of_all_elements_located
猜你喜欢
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多