Reputation: 47
I am trying to download a web page with python 2.7.13 and go line by line to analyze it. From memory and from searching around, I found that the following code snippet would be sufficient to go line by line:
with s as f:
for line in f:
print line
The variable s
is defined by a file.read() and the file is defined by urllib2 opening a specified url. Unfortunately, when I run the script, I get this syntax error:
Traceback (most recent call last):
File "a.py", line 12, in <module>
with s as f:
AttributeError: __exit__
I'm honestly dumbfounded of what I did wrong and it would be appreciated to gain insight about my mistake.
Upvotes: 0
Views: 100
Reputation: 191844
I found that the following code snippet would be sufficient to go line by line:
It is, but for file objects as in
with open(filename) as f:
for line in f:
The variable s is defined by a file.read()
That's a string, and can't be used in the with
.
and the file is defined by urllib2 opening a specified url.
That's a file-like object, but happens to be iterable
I am trying to download a web page with python 2.7.13 and go line by line to analyze it.
No useful information can be gathered from iterating lines of a website. (at least (X)HTML, JSON, etc)
Try using BeautifulSoup or XPath
Upvotes: 2