write a code for python to automatically copy the content of the…
- User
write a code for python to automatically copy the content of the “https://www.investing.com/crypto/bitcoin/historical-data” web address into a text file
- Assistant
To copy the content of a webpage into a text file using Python, you can use the
requestslibrary to send an HTTP request and retrieve the webpage's content. Then, you can write the content into a text file using the built-in file operations.Here's an example code that achieves this:
import requests url = "https://www.investing.com/crypto/bitcoin/historical-data" output_file = "output.txt" # Send HTTP GET request and retrieve the webpage content response = requests.get(url) content = response.text # Write the content into a text file with open(output_file, "w", encoding="utf-8") as file: file.write(content) print(f"Content copied to '{output_file}' successfully!")In this code, the
requests.get()function sends an HTTP GET request to the specified URL, andresponse.textretrieves the response content as a string. Then, the content is written into a text file using theopen()function with"w"mode, and the file is closed automatically using awithstatement.Make sure you have the library installed. You can install it via pip, if needed: