0

Below is the code

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()

So this is the code for saving the file in HTML format. But I can't find the way to get the contents of for loop into HTML file. In other words, I want to write a list of indexes elements i.e. http://google.com, http://stackoverflow.com into my HTML file. As you can see that I have created myfile.html as HTML file, So I want to write both URLs which are in the list of indexes into my HTML file

Hope this time I better explain? How can I? Would anyone like to suggest me something? It would be a really big help.

2
  • Do you want to create separate HTML files and add sitenames as a title? Commented Aug 8, 2018 at 12:21
  • Very appreciated that you replied. Thanks for it. I have actually edited my question please see, if you still have any confusion, please let me know. Commented Aug 8, 2018 at 13:55

1 Answer 1

1

Try below code:

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Work like a charm, I don't know how? Really. Any resource to learn?
Hmm... Actually, it's quite simple: you leave a string placeholder %s inside whole and whole % ", ".join(urls) means join strings from list urls with ", " separator into single string and substitute %s placeholder with resulting string. I don't know what exactly you might need to learn additionally
Thanks for it. @Andersson
Hi, @Andersson This is my new question on selenium based and you will get complete info about it. Please look for at least once. stackoverflow.com/questions/52082123/… Thanks:)
Hi Andersson, I have a new edited question I want to know is my code will work efficiently or not. So I need you help on that. Here is the stackoverflow.com/questions/46642992/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.