Windows 7 CMD中查看Python完整输出及导出HTML内容方法求助
Got it, let's tackle your two questions one by one—this is a super common pain point when working with large text outputs in Python's interactive CMD shell on Windows 7.
r.text When you just type r.text in the shell, CMD can only show the last few hundred lines because of its scrollback limit. Here are two easy fixes:
Method 1: Use CMD's built-in pager tool
You can write the HTML content to a temporary file first, then use themorecommand to scroll through it page by page directly from Python:import os # Write the full HTML content to a temp file with open("temp_viewer.html", "w", encoding="utf-8") as f: f.write(r.text) # Launch CMD's more command to view content incrementally os.system("more temp_viewer.html")Once you're done, you can delete the
temp_viewer.htmlfile manually—it'll be in the same folder where you started the Python shell.Method 2: Select and copy all output
If you don't want to create a file, right-click the CMD window's title bar, go to Edit > Mark, then click and drag your mouse from the top of the output (even if it's scrolled off screen) to the bottom. Press Enter to copy the entire content, then paste it into Notepad or any text editor to read everything.
r.text to a new HTML file This is straightforward with Python's built-in file handling. Just run this code in the interactive shell:
# Replace "my_webpage.html" with your preferred filename with open("my_webpage.html", "w", encoding="utf-8") as html_file: html_file.write(r.text)
- Always include
encoding="utf-8"to prevent garbled characters, especially if the webpage has non-English text or special symbols. - After running this, you'll find the HTML file in your current working directory (the folder you were in when you opened CMD). You can even double-click it to open the rendered page in your browser!
内容的提问来源于stack exchange,提问作者Petris

