【问题标题】:How to write for loop content in html code python?如何在html代码python中编写for循环内容?
【发布时间】:2018-08-04 20:53:48
【问题描述】:

下面是代码

urls.append('http://google.com')
urls.append('http://stacoverflow.com')
 whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s    // here I want to write both urls.
</body>
</html>"""
for x in urls:
    print x
f = open('myfile.html', 'w')
f.write(whole)
f.close()

这就是将文件保存为 HTML 格式的代码。但我找不到将 for 循环的内容放入 HTML 文件的方法。换句话说,我想在我的 HTML 文件中写入索引元素列表,即http://google.com, http://stackoverflow.com。如您所见,我已将 myfile.html 创建为 HTML 文件,因此我想将索引列表中的两个 URL 写入我的 HTML 文件

希望这次我能更好地解释一下吗? 我怎样才能?有人愿意给我一些建议吗?这将是一个非常大的帮助。

【问题讨论】:

  • 您要创建单独的 HTML 文件并将站点名称添加为title吗?
  • 非常感谢您的回复。谢谢你。我实际上已经编辑了我的问题,请看,如果您仍有任何困惑,请告诉我。

标签: python html python-2.7


【解决方案1】:

试试下面的代码:

urls.append('http://google.com')
urls.append('http://stacoverflow.com')
whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s
</body>
</html>"""
f = open('myfile.html', 'w')
f.write(whole % ", ".join(urls))
f.close()

【讨论】:

  • 工作就像一个魅力,我不知道怎么做?真的。有什么资源可以学习吗?
  • 嗯...实际上,这很简单:在wholewhole % ", ".join(urls) 中留下一个字符串占位符%s 表示将列表urls 中的字符串与", " 分隔符成单个字符串并用结果字符串替换%s 占位符。我不知道你可能还需要学习什么
  • 谢谢。 @安德森
  • 嗨,@Andersson 这是我关于基于硒的新问题,您将获得有关它的完整信息。请至少查找一次。 stackoverflow.com/questions/52082123/… 谢谢:)
  • 嗨安德森,我有一个新的编辑问题,我想知道我的代码是否能有效工作。所以我需要你的帮助。这是stackoverflow.com/questions/46642992/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
  • 2022-01-27
相关资源
最近更新 更多