首页 > 解决方案 > 如何使用 BeautifulSoup 逐行读取数据?

问题描述

我有以下代码,它为我提供了 Example.html 文件中的数据。但我必须逐行读取数据

html_doc = open("Example.html","r")
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())

标签: pythonbeautifulsoup

解决方案


您可以使用 splitlines() 方法轻松地逐行读取数据。

对于您的情况,您可以使用:

html_doc = open("Example.html","r")
soup = BeautifulSoup(html_doc, 'html.parser')
output = soup.get_text()

for row in output.splitlines():
  # Do whatever you want

编辑:对于您在 if 条件后读取 20 行的请求,您可以枚举splitlines()方法并读取接下来的 20 行。然后,使用break语句退出 for 循环。

for idx, row in enumerate(output.splitlines()):
    if row == "ADD.c":
       twenty_line = idx + 20
    try:
       if idx < twenty_line:
          print(row + "\n")
       else:
          break
    except NameError as e:
        print(e)

推荐阅读