【问题标题】:Amazon Web scrapping:Error Message: no such element: unable to locate element: [duplicate]亚马逊网络抓取:错误消息:没有这样的元素:无法找到元素:[重复]
【发布时间】:2020-06-07 09:33:34
【问题描述】:

我正在做一个课堂项目,使用 BeautifulSoup 和 webdriver 在亚马逊上抓取一次性尿布,以获取商品名称、价格、评论、评级。

我的目标是拥有这样的东西:

       Diapers Size 4, 150 Count - Pampers Swaddlers Disposable Baby Diapers, One Month Supply
       4.0 out of 5 stars
       1,982
       $43.98
      ($0.29/Count)

不幸的是,我在出现 50 个数据后收到此消息:

消息:没有这样的元素:无法找到元素:{“method”:“css 选择器","选择器":".a-last"}

这是我的代码:

    URL = "https://www.amazon.com/s?k=baby+disposable&rh=n%3A166772011&ref=nb_sb_noss"
    driver = ('C:/Users/Desktop/chromedriver_win32/chromedriver.exe')
    driver.get(URL)
    html = driver.page_source
    soup = BeautifulSoup(html, "html.parser")
    df = pd.DataFrame(columns = ["Product Name","Rating","Number of Reviews","Price","Price Count"])

    while True:
      for i in soup.find_all(class_= "sg-col-4-of-24 sg-col-4-of-12 sg-col-4-of-36 s-result-item sg-col-
      4-of-28 sg-col-4-of-16 sg-col sg-col-4-of-20 sg-col-4-of-32"):
      ProductName = i.find(class_= "a-size-base-plus a-color-base a-text-normal").text#.span.get_text
      print(ProductName)
      try:
      Rating = i.find(class_= "a-icon-alt").text#.span.get_text()
      except:
         Rating = "Null"
        print(Rating)
        try:
       NumberOfReviews = i.find(class_= "a-size-base").text#.span.get_text()
       except:
       NumberOfReviews = "Null"
       print(NumberOfReviews)
       try:
       Price = i.find(class_= "a-offscreen").text#.span.get_text()
       except:
       Price = "Null"
       print(Price)
       try:
       PriceCount = i.find(class_= "a-size-base a-color-secondary").text#.span.get_text()
       except:
       PriceCount = "Null"
       print(PriceCount)
       df = df.append({"Product Name":ProductName, "Rating":Rating, "Number of Reviews":NumberOfReviews, 
      "Price":Price, "Price Count":PriceCount}, ignore_index = True)
       nextlink = soup.find(class_= "a-disabled a-last")
       if nextlink:
       print ("This is the last page. ")
       break
       else:
       progress = driver.find_element_by_class_name('a-last').click()
       subhtml = driver.page_source
       soup = BeautifulSoup(subhtml, "html.parser")

不幸的是,我撞到了一条街区,试图弄清楚为什么它没有采用a_last

【问题讨论】:

    标签: python selenium web-scraping beautifulsoup selenium-chromedriver


    【解决方案1】:

    出现此错误是因为尚未在网页上加载 web 元素。 在对 web 元素执行操作之前,您需要确保该元素存在/加载在网页上。 你怎么做到这一点? 通过实现各种方法来同步/等待。 隐式、显式、流畅 等待 - 您可以使用这些方法中的任何一种来等待“最后一个”首先出现,然后单击它。 对于您的代码,您可以使用显式等待:

        WebDriverWait wait = new WebDriverWait(driver,30);
        WebElement elementToClick = wait.until(ExpectedConditions. elementToBeClickable(driver.find_element_by_class_name('a-last');
        elementToClick.click();
    

    【讨论】:

    • 感谢@Ashwini 的回答。该代码在设置睡眠时间后确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多