【问题标题】:How to scrape a website which changes its element on scroll? [closed]如何抓取在滚动时更改其元素的网站? [关闭]
【发布时间】:2021-02-16 03:01:14
【问题描述】:

This 是我试图从中获取数据的链接之一。此站点在滚动到第 8 个元素后更改其产品元素。 就像在搜索查询后给出了 200 多个产品,但页面源仅显示 8 个,当您滚动页面时,这 8 个更改为接下来的 8 个产品。结果,即使在滚动到结束或呈现页面之后,我也只能获得 8 个产品元素。任何人都知道如何获得所有产品?

【问题讨论】:

    标签: javascript python selenium web web-scraping


    【解决方案1】:

    该页面仅显示少量产品。

    您可以查看向下滚动时发出的请求。

    例如:

    这是第一次加载页面时发生的第一个请求,注意查询参数skip=0

    https://www.lkqonline.com/api/catalog/0/product?catalogId=0&year=2020&make=ACURA&model=ILX&category=Engine%20Compartment%7CEngine%20Assembly&sort=closestFirst&skip=0&take=12&latitude=32.81556&longitude=34.98917
    

    滚动一点后下一个请求是

    https://www.lkqonline.com/api/catalog/0/product?catalogId=0&year=2020&make=ACURA&model=ILX&category=Engine%20Compartment%7CEngine%20Assembly&sort=closestFirst&skip=12&take=12&latitude=32.81556&longitude=34.98917
    

    如您所见,唯一的区别是 skip 参数,现在等于 12。

    因此,要获得所有产品,您似乎必须提出这些请求,并将 skip 参数增加 12 直到您到达终点。

    【讨论】:

    • 我无法发送这样的直接获取请求
    • 为什么不呢?如果您正在抓取该网站,则无论如何您已经在向该域发出请求。因此,与其请求整个页面然后抓取,只需请求产品的特定网址并完全避免抓取。
    【解决方案2】:

    您可以尝试让 selenium 为您“向下滚动”。这是代码。 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 向下滚动时,html 结构会发生变化。

    现在的问题是什么时候应该停止滚动?对于您的网站,它是一个有限的高度(这意味着您可以点击到页面底部)。所以你可能需要编写一个小的 while 循环来保持向下滚动直到它到达底部。

    【讨论】:

      【解决方案3】:

      使用无限页面滚动并打印我们可以实现的值。请求会更好,但不允许或不想要。

      driver.get('https://www.lkqonline.com/2020-Acura-Ilx-Engine-Assembly')
      scroll_pause_time = 1 
      screen_height = driver.execute_script("return window.screen.height;")   # get the screen height of the web
      i = 1
      
      while True:
          # scroll one screen height each time
          elems=driver.find_elements_by_css_selector('div.full-side')
          for elem in elems:
              print(elem.text)
          driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))  
          i += 1
          time.sleep(scroll_pause_time)
          # update scroll height each time after scrolled, as the scroll height can change after we scrolled the page
          scroll_height = driver.execute_script("return document.body.scrollHeight;")  
          # Break the loop when the height we need to scroll to is larger than the total scroll height
          if (screen_height) * i > scroll_height:
              break
      

      输出

      2020 Acura Ilx Used Engine Assembly - ~236203268
      (2.4L, VIN 2, 6th digit)
      Mileage: 33k
      Location: SUMNER, WA
      Source: 2016 Acura Ilx
      Price: $1,592.00
      2020 Acura Ilx Used Engine Assembly - ~261985457
      (2.4L, VIN 2, 6th digit)
      Mileage: 106k
      Location: RANCHO CORDOVA, CA
      Source: 2016 Acura Ilx
      Price: $1,366.00
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 2013-11-11
        相关资源
        最近更新 更多