Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102058

Re: Question about how to do something in BeautifulSoup?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Cody Piersall <cody.piersall@gmail.com>
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 <mailman.190.1453582197.15297.python-list@python.org> (permalink)
References <CAPeSgWorAAUz7Enkbrkfiuiu5MF9U4dQo0EBCvgvzCcYFru7Zw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de ugg0gVPdAoeBlVBT6Y48DQ7/UOzQVhR7xK67QcFPd4Fw==
Return-Path <cody.piersall@gmail.com>
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; '&lt;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; '&gt;': 0.18; 'email addr:gmail.com&gt;': 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 <CAPeSgWorAAUz7Enkbrkfiuiu5MF9U4dQo0EBCvgvzCcYFru7Zw@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102058

Show key headers only | View raw


On Fri, Jan 22, 2016 at 8:01 AM, inhahe <inhahe@gmail.com> wrote:
> Say I have the following HTML (I hope this shows up as plain text here
> rather than formatting):
>
> <div style="font-size: 20pt;"><span style="color:
#000000;"><em><strong>"Is
> today the day?"</strong></em></span></div>
>
> And I want to extract the "Is today the day?" part. There are other places
> in the document with <em> and <strong>, 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 <strong> is within the <em> and sometimes the <em> is
> within the <strong>.
> - 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('''\
<div style="font-size: 20pt;"><span style="color: #000000;"><em><strong>"Is
today the day?"</strong></em></span></div>
<div style="font-size: 20pt;"><span style="color: RGB(0, 0,
0);"><strong><em>"Is
tomorrow the day?"</em></strong></span></div>''')

# 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about how to do something in BeautifulSoup? Cody Piersall <cody.piersall@gmail.com> - 2016-01-23 14:49 -0600

csiph-web