python - Python-for循环x次,循环中包含信息
问题描述
我正在尝试循环遍历一条信息 ax 次,但我似乎无法使其与 range() 或 isslice 一起使用。我想说循环中的代码只循环了 x 次。
我想循环通过 x 次的循环:
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
x = soup.find("div", class_="object-list-items-container")
for child in x.findChildren("section", recursive=False):
if "U heeft gereageerd" in child.text:
continue
else:
house_id = child.find("div", {'class': 'ng-scope'}).get("id")
driver.find_element_by_id(house_id).click()
我已经阅读了很多堆栈溢出问题,但我可能没有足够的经验来针对我的情况实施它。我已经尝试了几件事,但到目前为止没有任何效果。
我尝试了以下方法:
(“reacties”是它需要循环的 x 次的变量)
for i in range(reacties):
for child in x.findChildren("section", recursive=False):
if "U heeft gereageerd" in child.text:
continue
else:
...........
和:
for i in range(reacties):
child= x.findChildren("section", recursive=False)
if "U heeft gereageerd" in child.text:
continue
else:
...............
解决方案
您可以合并enumerate
功能。
https://docs.python.org/3/library/functions.html#enumerate
例如:
for count, child in enumerate(x.findChildren("section", recursive=False)):
If count > reacties:
break
if "U heeft gereageerd" in child.text:
continue
else:
...
推荐阅读
- javascript - 在 Vue.js 中动态创建元素
- java - 无法将 MySQL 连接到我的 Spring Boot 项目(java.sql.SQLException: Access denied for user 'root'@'localhost')
- python - Python中Matplotlib中日期时间的错误年份
- python - 如何使用带有 oAuth 2.0 的 Basecamp 2 API 作为最终用户发送评论
- http - HTTP 403 禁止消息格式
- reactjs - 有没有办法在 React 中将 API 数据从一个文件导入另一个文件?
- rust - 我收到“parquet-schema:找不到命令”,尽管我已经完成了 cargo install parquet
- windows - Windows 上的 UIautomation:如何获取窗口元素的元素名称(缩放音频图标)
- amazon-web-services - 保留 Registrar 的 DNS 服务但指向 AWS 资源?
- haskell - 如何修改这个 Haskell 函数,这样我就不必导入 Data.Bool 并且只使用前奏函数?