Sublime Text 3运行Python爬虫代码输出异常,在线Trinket运行正常的问题求助
Hey there! Let's figure out why your attendance scraper isn't showing the "hiii" message correctly in Sublime Text 3, even though it works perfectly on Trinket. I've run into similar output quirks with Sublime before, so here's what's likely going on and how to fix it without switching IDEs.
Possible Causes
Buffered Output
Sublime Text's default Python build system uses buffered output—meaning it holds onto printed content until the buffer is full before displaying it. Trinket, on the other hand, usually flushes output in real-time. So your "hiii" messages might be stuck in the buffer instead of showing up immediately.Encoding or Hidden Character Conflicts
The scraped<td>content might have hidden whitespace, newlines, or encoding mismatches that are messing up how Sublime displays the output, making it look like "hiii" is missing even though it's being printed.
Fixes You Can Try Right Now
1. Force Output Flush (Quickest Fix)
Modify your print statements to force the buffer to flush immediately. Just add flush=True to the print calls:
print(x.getText()) print("hiii", flush=True)
Alternatively, you can import sys and manually flush after each print:
import sys # ... rest of your code ... for x in result: print(x.getText()) print("hiii") sys.stdout.flush()
This ensures every line gets printed right away instead of waiting for the buffer to fill.
2. Create an Unbuffered Build System in Sublime
If you want a permanent fix for all your Python scripts in Sublime:
- Go to
Tools > Build System > New Build System - Replace the default content with this:
{ "cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"} }
(Note: Use python3 instead of python if that's how you call Python 3.8 on your system)
- Save it as
Python-Unbuffered.sublime-build(save it in the User folder when prompted) - Now select this build system from
Tools > Build System > Python-Unbufferedand run your script again. The-uflag disables output buffering entirely.
3. Clean Up Scraped Text
Sometimes messy whitespace in the scraped content can push "hiii" out of view. Clean the text first with strip() to remove extra spaces/newlines:
for x in result: clean_content = x.getText().strip() print(clean_content) print("hiii")
This makes your output neater and ensures "hiii" shows up right after the course/attendance text.
Give these steps a shot—buffering is almost certainly the culprit here, so the first two fixes should resolve the issue quickly.
内容的提问来源于stack exchange,提问作者TUSHAR YADAV

