【问题标题】:How to get Source code of html file and put in string [duplicate]如何获取html文件的源代码并放入字符串[重复]
【发布时间】:2015-10-29 14:38:09
【问题描述】:

是否可以将 HTML 文件的源代码放入变量中?

我的意思是,如果我有test.html,那么我该如何获取这个html文件的源代码并将其放在一个字符串上?

【问题讨论】:

    标签: python html


    【解决方案1】:

    只需打开文件并阅读:

    f = open('test.html', 'r')
    html_string = f.read()
    f.close()
    

    with open('test.html', 'r') as f:  # with can auto close the file like f.close() does
        html_string = f.read()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2012-02-15
      相关资源
      最近更新 更多