Help on instance of closeable_response in module Mechanize
Chris Rebert
crebert at ucsd.edu
Mon Aug 22 20:27:18 EDT 2011
On Mon, Aug 22, 2011 at 5:17 PM, Yingjie Lin <Yingjie.Lin at mssm.edu> wrote:
> Hi Python users,
>
> I have a question about the instance of closeable_response in module Mechanize.
>
> from mechanize import ParseResponse, urlopen
> url = "http://wwwsearch.sourceforge.net/mechanize/example.html"
> r = urlopen(url)
> forms = ParseResponse(r, backwards_compat=False)
> html_lines = r.read()
>
> If I call ParseResponse() before r.read(), then lforms would be a list containing one form
> instance, and html_lines would be an empty string. If I call r.read() first, then html_lines
> would be the HTML source code of the page, but forms would be an empty list.
>
> Therefore, I have to open the url twice, once for each function, like this:
>
> r = urlopen(url)
> forms = ParseResponse(r, backwards_compat=False)
> r = urlopen(url)
> html_lines = r.read()
>
> I believe this shouldn't be necessary. What is the proper way of doing it? Thank you.
Untested speculation:
from StringIO import StringIO
r = urlopen(url)
html = r.read()
s = StringIO(html)
forms = ParseResponse(s, backwards_compat=False)
Cheers,
Chris
More information about the Python-list
mailing list