Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:sourceforge': 0.02; 'string.': 0.04; 'subject:module': 0.04; 'instance': 0.05; 'instance,': 0.05; 'function,': 0.07; 'mechanize': 0.07; 'python': 0.08; 'users,': 0.13; 'stringio': 0.16; 'subject:instance': 0.16; 'url:mechanize': 0.16; 'url:wwwsearch': 0.16; 'cc:addr:python- list': 0.16; 'mon,': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'subject:Help': 0.17; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; "shouldn't": 0.23; 'pm,': 0.24; 'aug': 0.24; 'code': 0.25; 'skip:b 20': 0.26; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; '22,': 0.30; 'chris': 0.32; 'list': 0.32; 'necessary.': 0.32; 'source': 0.33; 'it?': 0.33; 'received:132': 0.34; 'list.': 0.35; 'thank': 0.35; 'question': 0.36; 'doing': 0.36; 'but': 0.37; 'open': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'empty': 0.39; 'url:net': 0.60; 'you.': 0.62; 'believe': 0.65; 'therefore,': 0.66; 'url:example': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=ucsd.edu; i=crebert@ucsd.edu; q=dns/txt; s=041709-iport; t=1314059311; x=1345595311; h=mime-version:in-reply-to:references:date:message-id: subject:from:to:cc:content-transfer-encoding; z=MIME-Version:=201.0|In-Reply-To:=20<8199DBA0-5A00-4932-9 B7C-A5BAD8EC6EEA@mssm.edu>|References:=20<8199DBA0-5A00-4 932-9B7C-A5BAD8EC6EEA@mssm.edu>|Date:=20Mon,=2022=20Aug =202011=2017:27:18=20-0700|Message-ID:=20 |Subject:=20Re:=20Help=20on=20instance=20of=20closeable_r esponse=20in=20module=20Mechanize|From:=20Chris=20Rebert =20|To:=20Yingjie=20Lin=20|Cc:=20python-list@python.org |Content-Transfer-Encoding:=20quoted-printable; bh=nzKDW2y+jimuBYIprMheKeN+/0MHlxiLSRx4gytDo+U=; b=EHH/3LtleocFSWrYGYnKmwAFyHiVY40s6R1CcibBX64ltzCsmNAEjNwH m5SBdc1G1Hs+rNZ5EkWOCxPZQCYd424PSX/dhfxgBNH5oQDC3i7FcEORE QAS46I7hBvaj7dYTQLZi+PaObLPNPepkMPZr+f5fgV28lF9ltccI9KgHe E=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgEFAAjzUk6E7/kU/2dsb2JhbABBDoQ9o08Id4FAAQEBAQMSAg9WEAkCCw0CAiYCAiISAQUBHAYTIodTl2AKi3mIcYh/gSyEDIEQBIdgizaEfYM/hB48gytY X-IronPort-AV: E=Sophos;i="4.68,266,1312182000"; d="scan'208";a="587858019" X-Spam-Status: No X-Spam-Level: MIME-Version: 1.0 In-Reply-To: <8199DBA0-5A00-4932-9B7C-A5BAD8EC6EEA@mssm.edu> References: <8199DBA0-5A00-4932-9B7C-A5BAD8EC6EEA@mssm.edu> Date: Mon, 22 Aug 2011 17:27:18 -0700 Subject: Re: Help on instance of closeable_response in module Mechanize From: Chris Rebert To: Yingjie Lin Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314059312 news.xs4all.nl 23979 [2001:888:2000:d::a6]:34700 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12057 On Mon, Aug 22, 2011 at 5:17 PM, Yingjie Lin wrote: > Hi Python users, > > I have a question about the instance of closeable_response in module Mech= anize. > > =C2=A0 =C2=A0 =C2=A0 =C2=A0from mechanize import ParseResponse, urlopen > =C2=A0 =C2=A0 =C2=A0 =C2=A0url =3D "http://wwwsearch.sourceforge.net/mech= anize/example.html" > =C2=A0 =C2=A0 =C2=A0 =C2=A0r =3D urlopen(url) > =C2=A0 =C2=A0 =C2=A0 =C2=A0forms =3D ParseResponse(r, backwards_compat=3D= False) > =C2=A0 =C2=A0 =C2=A0 =C2=A0html_lines =3D r.read() > > If I call ParseResponse() before r.read(), then lforms would be a list co= ntaining one form > instance, and html_lines would be an empty string. If I call r.read() fir= st, then html_lines > would be the HTML source code of the page, but forms would be an empty li= st. > > Therefore, I have to open the url twice, once for each function, like thi= s: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0r =3D urlopen(url) > =C2=A0 =C2=A0 =C2=A0 =C2=A0forms =3D ParseResponse(r, backwards_compat=3D= False) > =C2=A0 =C2=A0 =C2=A0 =C2=A0r =3D urlopen(url) > =C2=A0 =C2=A0 =C2=A0 =C2=A0html_lines =3D 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 =3D urlopen(url) html =3D r.read() s =3D StringIO(html) forms =3D ParseResponse(s, backwards_compat=3DFalse) Cheers, Chris