You can write a string to a text file like this:
with open("yourfile.txt", "w") as f:
f.write(yourstr)
If you don't want to override the file just use "a" as second parameter in open. See https://docs.python.org/2/library/functions.html#open.
So, I assume that you have a list of links like this:
["http://example.com", "http://stackoverflow.com"]
and you want to have a file like this:
http://example.com:
<!doctype html>
<html>
<body>
...
<h1>Example Domain</h1>
...
</body>
</html>
http://stackoverflow.com:
...
Let's start with iterating all the links:
for url in yourlinks:
First, you want to write the url to the file:
with open("yourfile.txt", "a") as f:
f.write(url+"
") # the
is a new line
Now you download the content of the website to a variable:
content = urllib2.urlopen(url).read()
(It may be that there are errors because of encoding - I'm from python3.)
And you write it to the file:
f.write(content+"
")
Voila! You should have your file now.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…