Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'suppose': 0.05; 'prints': 0.07; 'revision': 0.09; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail-iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'bad:': 0.16; 'subject:Trying': 0.16; 'useful,': 0.16; 'bytes': 0.19; 'object,': 0.19; 'header:In-Reply-To:1': 0.21; "wasn't": 0.22; 'parse': 0.23; 'extract': 0.25; 'string': 0.26; 'received:209.85.214': 0.28; 'import': 0.29; 'consistently': 0.29; 'listing': 0.31; "skip:' 10": 0.32; 'expression': 0.32; 'to:addr :python-list': 0.33; 'list': 0.33; 'actually': 0.33; 'regular': 0.34; 'header:User-Agent:1': 0.35; 'using': 0.35; 'message- id:@gmail.com': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'skip:r 30': 0.37; 'page': 0.37; 'pretty': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'received:192': 0.38; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'received:192.168.1': 0.40; 'below': 0.61; 'links:': 0.67; 'document.': 0.84; 'num': 0.84; 'rev.': 0.84; 'url:dir': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=aUQ1KGCRY5N4ZBVJjiMoA3xZnhefXGWGJWDy7hw4iho=; b=OxElOAhOeGnW+xfqikdV9uswl1AxmTwM1teK70DFLT4G0ols/AogbUyoB2B1YpkNHa 9Ihx9cVuZmj+kwizWrcPHu4VGiup57VksY1X/oqs9eFadUXjddrIZRJ+TZdkFmExKM4D +uHrfxVYhU1seJ4PYWx+ZOVsnQvEXYkZEJyck= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; b=Jwx0koPXKF0LBnOtoVVq3xd1i5IGrlg7O4nHxmc2THLQnblLODykIKo15Vw4A/LBl5 JSBuLpB7cWzFX0+OCmsd+yI2c71bB0WUkLIdk10ijpEWaByIMpt5byANcZuoMyBINxnQ 4wbnYPlFEQTC7M57zzWb7UYyXKOoZEfGzmPV4= Date: Thu, 19 May 2011 16:35:09 -0500 From: Andrew Berg User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Trying to understand html.parser.HTMLParser References: <4DD03B69.6050301@gmail.com> <4DD0D1A2.6060109@free.fr> In-Reply-To: <4DD0D1A2.6060109@free.fr> X-Enigmail-Version: 1.1.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 23 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305840917 news.xs4all.nl 49039 [::ffff:82.94.164.166]:56186 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5790 On 2011.05.16 02:26 AM, Karim wrote: > Use regular expression for bad HTLM or beautifulSoup (google it), below > a exemple to extract all html links: Actually, using regex wasn't so bad: > import re > import urllib.request > > url = 'http://x264.nl/x264/?dir=./64bit/8bit_depth' > page = str(urllib.request.urlopen(url).read(), encoding='utf-8') # > urlopen() returns a bytes object, need to get a normal string > rev_re = re.compile('revision[0-9][0-9][0-9][0-9]') > num_re = re.compile('[0-9][0-9][0-9][0-9]') > rev = rev_re.findall(str(page))[0] # only need the first item since > the first listing is the latest revision > num = num_re.findall(rev)[0] # findall() always returns a list > print(num) prints out the revision number - 1995. 'revision1995' might be useful, so I saved that to rev. This actually works pretty well for consistently formatted lists. I suppose I went about this the wrong way - I thought I needed to parse the HTML to get the links and do simple regexes on those, but I can just do simple regexes on the entire HTML document.