2

I'm saving HTTP request as an HTML page. How can I save the HTML file with the name of the URL.

I'm using Linux OS

So the file name will look like this: "http://www.test.com.html"

My code:

url = "http://www.test.com"
page = urllib.urlopen(url).read()
f = open("./file.html", "w")
f.write(page)
f.close()
bugnet17
  • 173
  • 1
  • 1
  • 12

1 Answers1

0

Unfortunately you cannot save a file with a url name. The character "/" is not allowed in Windows file names.

However, you can create a file with the name www.test.com.html with the following line

file_name = url.split('/')[2]

If you need to anything like https://www.test.com/posts/1, you can try to replace / with another custom character that usually not occurs in url such as __

url = 'https://www.test.com/posts/11111111'
file_name = '__'.join(url.split('/')[2:])

Will result in

www.test.com__posts__1
BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42