diff options
author | Leonard Richardson <leonardr@segfault.org> | 2016-07-26 21:27:28 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2016-07-26 21:27:28 -0400 |
commit | aaf42d1ce88c98c82560cfabc41186c8f82e05f0 (patch) | |
tree | 2ea591179eecc1ca8ddca7b2afab033db4758237 | |
parent | 28098ad3845795283b9c1314228f5f8af4377dee (diff) | |
parent | c420c44874d07b7383beacfbfcb084dc8be14d68 (diff) | |
download | beautifulsoup4-aaf42d1ce88c98c82560cfabc41186c8f82e05f0.tar.gz |
Change the way open() is used. Code contributed by Ville Skyttä.
-rw-r--r-- | bs4/diagnose.py | 3 | ||||
-rw-r--r-- | doc/source/index.rst | 6 |
2 files changed, 6 insertions, 3 deletions
diff --git a/bs4/diagnose.py b/bs4/diagnose.py index cda2f01..8768332 100644 --- a/bs4/diagnose.py +++ b/bs4/diagnose.py @@ -58,7 +58,8 @@ def diagnose(data): data = data.read() elif os.path.exists(data): print '"%s" looks like a filename. Reading data from the file.' % data - data = open(data).read() + with open(data) as fp: + data = fp.read() elif data.startswith("http:") or data.startswith("https:"): print '"%s" looks like a URL. Beautiful Soup is not an HTTP client.' % data print "You need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup." diff --git a/doc/source/index.rst b/doc/source/index.rst index 8258e97..a6b2076 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -298,7 +298,8 @@ constructor. You can pass in a string or an open filehandle:: from bs4 import BeautifulSoup - soup = BeautifulSoup(open("index.html")) + with open("index.html") as fp: + soup = BeautifulSoup(fp) soup = BeautifulSoup("<html>data</html>") @@ -2776,7 +2777,8 @@ you how different parsers handle the document, and tell you if you're missing a parser that Beautiful Soup could be using:: from bs4.diagnose import diagnose - data = open("bad.html").read() + with open("bad.html") as fp: + data = fp.read() diagnose(data) # Diagnostic running on Beautiful Soup 4.2.0 |