0

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.

Yetoo
  • 49
  • 1
  • 8

1 Answers1

2

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

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • Alright, I just remembered I could split the lines by "\n" and the loop through the list. Like I said, I'm very foggy today. Thanks for your help. – Yetoo Sep 04 '17 at 21:33
  • You can, but unless your website only contains multi line text, then parsing HTML makes more sense – OneCricketeer Sep 04 '17 at 21:37