Save HTML of some website in a txt file with python

后端 未结 2 1636
梦毁少年i
梦毁少年i 2020-12-14 21:50

I need save the HTML code of any website in a txt file, is a very easy exercise but I have doubts with this because a have a function that do this:

import ur         


        
相关标签:
2条回答
  • 2020-12-14 22:08

    I use Python 3.
    pip install requests - after install requests library you can save a webpage in txt file.

    import requests
    
    url = "https://stackoverflow.com/questions/24297257/save-html-of-some-website-in-a-txt-file-with-python"
    
    r = requests.get(url)
    with open('file.txt', 'w') as file:
        file.write(r.text)
    
    0 讨论(0)
  • 2020-12-14 22:23

    Easiest way would be to use urlretrieve:

    import urllib
    
    urllib.urlretrieve("http://www.example.com/test.html", "test.txt")
    

    For Python 3.x the code is as follows:

    import urllib.request    
    urllib.request.urlretrieve("http://www.example.com/test.html", "test.txt")
    
    0 讨论(0)
提交回复
热议问题