【问题标题】:Python - How to read HTML line by line [duplicate]Python - 如何逐行读取 HTML [重复]
【发布时间】:2016-03-21 01:18:40
【问题描述】:

我正在尝试编写一个程序,它将获取一个 HTML 文件并输出每一行。我做错了什么,因为我的代码正在输出每个字母。如何将所有 HTML 行放入一个列表中?

这是目前为止的代码:

f = open("/home/tony/Downloads/page1/test.html", "r")
htmltext = f.read()
f.close()

for t in htmltext:
    print t + "\n"

【问题讨论】:

    标签: python html


    【解决方案1】:

    f.read() 将尝试读取并产生每个字符,直到遇到 EOF。你想要的是f.readlines() 方法:

    with open("/home/tony/Downloads/page1/test.html", "r") as f:
        for line in f.readlines():
            print(line) # The newline is included in line
    

    【讨论】:

      【解决方案2】:

      您可以使用f.readlines() 代替f.read()。此函数返回文件中所有行的列表。

      with open("/home/tony/Downloads/page1/test.html", "r") as f:
          for line in f.readlines():
              print(line)
      

      您也可以使用list(f)

      f = open("/home/tony/Downloads/page1/test.html", "r")
      f_lines = list(f)
      for line in f_lines:
          print(line)
      

      来源:https://docs.python.org/3.5/tutorial/inputoutput.html

      【讨论】:

        猜你喜欢
        • 2017-08-29
        • 1970-01-01
        • 2012-09-05
        • 1970-01-01
        • 2018-01-26
        • 1970-01-01
        • 2015-05-19
        • 2020-02-21
        • 1970-01-01
        相关资源
        最近更新 更多