【问题标题】:Load more option while Infinite scrolling Python web scraping在无限滚动 Python 网页抓取时加载更多选项
【发布时间】:2021-07-12 12:31:35
【问题描述】:

试图滚动浏览网页 like this 并抓取他们的公司名称和描述。一旦滚动在网页上达到静止点,我无法破解“加载更多”选项。我如何才能穿透“加载更多”并继续将内容存储在列表或 df 中,以便稍后解析?

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.cloudstack.org/")
time.sleep(2)
scroll_pause_time = 1
screen_height = driver.execute_script("return window.screen.height;")
i = 1

while True:
    
    driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))  
    i += 1
    time.sleep(scroll_pause_time)
    
    scroll_height = driver.execute_script("return document.body.scrollHeight;")  
  
    if (screen_height) * i > scroll_height:
        break

html_source = driver.page_source
data = html_source.encode('utf-8')

我尝试使用此方法单击加载更多内容,但之后我遇到了“ElementNotInteractableException”。

  load_more = driver.find_elements_by_class_name("next-selector")
    if load_more:
        load_more[0].click()

Docs that helped me but never didn't fix the probelm overall

【问题讨论】:

  • 我已经回答了你这个问题,但是你用我的回答删除了这个问题。

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


【解决方案1】:

你为什么不尝试刮过

https://www.cloudtango.org/list/?page=1

有一个page 参数可以根据需要进行更改。

还有其他参数,例如:

country=&service=&partner=&locality=&postal_town=&administrative_area_level_1=&administrative_area_level_2=&administrative_area_level_3=&autocomplete=&companyname=&head_office=&coordenades_lat=&coordenades_lng=&orderby=&order=

运行loop 到需要的页面,抓取页面并根据需要保存。不超过 200 页,但是

这里是演示代码:

driver = webdriver.Chrome()
i=0
while True:
    driver.get(f"https://www.cloudtango.org/list/?page={i}")
    i+=1
    if driver.title!="Where IT seekers find Cloud Service Providers - Cloudtango":
        break

我们正在使用无限循环,但我们每次都在检查标题。当我们到达终点时,无限循环将中断。

【讨论】:

  • 因为我不确定那里有多少页。我应该进行一个while循环还是在for循环中给出一个任意数字并运行?
  • @maconline 然后尝试做喜欢编辑的答案!
  • 我不知道该网站有多少页。我想读到最后一页。
  • @maconline 请检查它是否有效。如果当时有效,请不要忘记将此答案标记为已接受。
  • 肯定会的。代码在解析内容时停在中间。对于范围内的我(100):driver.get(url)html=driver.page_source data=html.encode('utf-8')soup=bs4.BeautifulSoup(data,'html-parser')fin=soup.find_all ('td', {'class': 'company'} for x in fin: res= each.find('img')['alt'] dat.append(x)
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 2012-09-13
  • 2017-08-31
  • 2023-03-08
  • 2019-01-20
  • 2022-11-19
  • 2015-05-06
相关资源
最近更新 更多