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.

6
  • Which version of Python takes as in the condition of a while loop? Commented Sep 4, 2017 at 21:11
  • Where did you get this code snippet from? Commented Sep 4, 2017 at 21:12
  • The 'as' keyword is for import statements and exceptions. In this case, you should just be able to loop directly over the file object with your for line in f:, no need for a while loop. Commented Sep 4, 2017 at 21:14
  • Your mistake is simple: that is not valid Python, and I don't now what you think you were accomplishing. Commented Sep 4, 2017 at 21:14
  • I found the example here: stackoverflow.com/a/8010133/4415644 Commented Sep 4, 2017 at 21:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You can, but unless your website only contains multi line text, then parsing HTML makes more sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.