Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cody Piersall Newsgroups: comp.lang.python Subject: Re: Question about how to do something in BeautifulSoup? Date: Sat, 23 Jan 2016 14:49:48 -0600 Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ugg0gVPdAoeBlVBT6Y48DQ7/UOzQVhR7xK67QcFPd4Fw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Question': 0.05; '22,': 0.09; 'positional': 0.09; 'span': 0.09; 'jan': 0.11; 'argument': 0.15; '"is': 0.16; '<div': 0.16; '2016': 0.16; 'hex': 0.16; 'itertools': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:d 60': 0.16; 'soup': 0.16; 'started:': 0.16; 'wrote:': 0.16; 'nested': 0.18; 'try:': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; 'to:2**1': 0.21; 'color,': 0.22; 'am,': 0.23; 'defined': 0.23; '(or': 0.23; "haven't": 0.24; 'import': 0.24; 'plain': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'specify': 0.27; 'that.': 0.30; "we're": 0.30; 'noticed': 0.32; 'extract': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'text': 0.35; 'done': 0.35; 'sometimes': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'say': 0.37; 'received:209': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'hope': 0.61; 'within': 0.64; 'places': 0.64; 'today': 0.65; 'subject:skip:B 10': 0.66; 'here': 0.66; 'color': 0.67; '8bit%:21': 0.70; 'levels': 0.70; 'tags,': 0.79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=QURVyxFLaPcPGq4bD6eTaR0FB2+JvlQA31X7w8hgsB0=; b=R4RhZTJWunTnQOrKFVSFjGsjVe56iViDyNxa4VusGSYSmxLnna+4hXG8iRYXBsMPWq aF0moq+HpCXkGRLEYvC1X1YEJMAYTQAmJDyYoIbvaPCAbVSQTcR1ix2a3OKUmgvkNiRD xCcVfpH/rwyttLH+X7bHMcQz5qPm+jXMumsLY6F74qHrPD870kRTjhmapX5cHy9rzjr+ 4ngFzzRBkKOpqmRBCSWjYY69HX6wnrCRp5Sc8tkncitE8+LbqgxVN/kckiVtBavcZiq8 dppvfUK3AAejed7cyppSIa5/vbPPT2SQH+vEZP0cJs6/AfZpkA4tCCgpcRhGHtHr6pUk ++Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=QURVyxFLaPcPGq4bD6eTaR0FB2+JvlQA31X7w8hgsB0=; b=mtRyTYyweoeLReS0KOGgPNGZcFtPw6OKGXlETy6GRrCLILEr5b9Zwz2/1Xfr/4JPq7 RRMdUecLS88l23xdvS/DDNh9lhzsVL4TazNknJbl6ccvXKEaBOfiNE2NmsVrnern+o8I a1A7zC+WQAOUQm3ST9P/qPLoHiFe8s+0nwzeOrdB7noc4c9WfnI4tIVsjtbpQNftnp5N QYh0Ufs3kxzYaMvtI7DaoBJY8eQWLG8YGhuwbNWscUlVx7gpvYK7BKXrp1S5M2Ad+LHL OVB82tGlgcoL67RNthKrbCkezwRXpO5dFTU07hNCkWBzfZBhx7kb9LYeSVovWknR4ne+ lR/g== X-Gm-Message-State: AG10YORLI/Cuf0rxFv1ShfU4j6EtUCph/pGrJBtnIpPyOpOizKgv32YCZEtDuAq7PIBDHpA8lXpIzPdJPbZAMA== X-Received: by 10.107.148.75 with SMTP id w72mr11633184iod.115.1453582188661; Sat, 23 Jan 2016 12:49:48 -0800 (PST) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102058 On Fri, Jan 22, 2016 at 8:01 AM, inhahe wrote: > Say I have the following HTML (I hope this shows up as plain text here > rather than formatting): > >
"Is > today the day?"
> > And I want to extract the "Is today the day?" part. There are other places > in the document with and , but this is the only place that > uses color #000000, so I want to extract anything that's within a color > #000000 style, even if it's nested multiple levels deep within that. > > - Sometimes the color is defined as RGB(0, 0, 0) and sometimes it's defined > as #000000 > - Sometimes the is within the and sometimes the is > within the . > - There may be other discrepancies I haven't noticed yet > > How can I do this in BeautifulSoup (or is this better done in lxml.html)? I hope this helps you get started: This may help you get started: from bs4 import BeautifulSoup from itertools import chain soup = BeautifulSoup('''\
"Is today the day?"
"Is tomorrow the day?"
''') # We're going to get all the tags that specify the color, either using hex or RGB. # If you only want to get the span tags, just give the positional argument 'span' to # find_all: # for tag in chain(soup.find_all('span', style='color: #000000;'), # soup.find_all('span', style='color: RGB(0, 0, 0);')): for tag in chain(soup.find_all(style='color: #000000;'), soup.find_all(style='color: RGB(0, 0, 0);')): try: print(tag.em.strong.text) except AttributeError: try: print(tag.strong.em.text) except AttributeError: print('ooooooh nooooo no text') Cody