python - 在无限滚动 Python 网页抓取时加载更多选项
问题描述
试图像这样滚动浏览网页并刮掉他们的公司名称和描述。一旦滚动在网页上达到静止点,我无法破解“加载更多”选项。我怎样才能穿透“加载更多”并继续将内容存储在列表或我可以稍后解析的 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()
解决方案
你为什么不试着刮过
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
我们正在使用无限循环,但我们每次都在检查标题。当我们到达终点时,无限循环将中断。
推荐阅读
- python - 无法在 Google Colab 中读取 3GB 文件
- django - 在 DJANGO 的框中为同一子类别创建两个用户
- android - 如何根据 kotlin 列表上的字符串参数输入过滤整数值?
- reporting-services - SSRS 如何将数据集字段的某个值提供给自定义代码中的变量
- forms - 如何多次提交表单(asp.net core razor)
- flutter - 当我向上滚动时,SliverAppBar 中的 FlexibleSpace 没有隐藏它的内容
- python - 如何在 mysql python 连接器中为列名放置占位符?
- e-commerce - 如何将 GA4 电子商务数据层添加到 WooCommerce?
- javascript - 最终没有出现在控制台代码末尾的问题
- python - 将数据框浮动到字符串 txt 文件