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


Groups > comp.lang.python > #26780

Re: Beautiful Soup Table Parsing

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andipersti@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.059
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; 'tom': 0.07; 'iterate': 0.09; 'andreas': 0.16; 'bye,': 0.16; 'row': 0.16; 'soup': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'do.': 0.21; 'received:mail- bk0-f46.google.com': 0.22; 'this:': 0.23; 'header': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'received:209.85.214.46': 0.27; 'figure': 0.30; 'code': 0.31; 'point': 0.31; 'not.': 0.32; 'could': 0.32; 'print': 0.32; 'getting': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'direction': 0.35; 'table': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'possible': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'received:10': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'first': 0.61; 'url:public': 0.62; 'below:': 0.71; 'url:page': 0.71; 'russell': 0.84; 'url:online': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=6zX55CmTsFJGqKGTuRDK+aMZQNb7uWzk/xFW60lr0cI=; b=bbH94Lg9rpClabB+er6dHlt6hL1oPgz1liNDnx9bFx4D5gSt8mDi0S8sf/vLKXtcPJ a7cIK7a7QucOpX8PfzShVGnxbVUoB1lbCORAsIXh0x0qPyiQWAHXBho4paJUY/rtOMpM eDCN6nTKMt5rnGxi6NjamkqX3rtqAs1fhEtcOQz512j02DrUcxkWlZBYXZcSjkzCpwlP YDXgBkDbNxYEM17NMykHzruxfdqTWzvV7Vuo4tEsk197QN8n/Jp6bDkiKLWiWSUvtvQZ 7RjAsrdZSCShVyYEWb8+RqeqEBN6wuTzCN2YielH0ZZmTHjvt0JnCvkVU1ZHfNIZt8ZT N1hw==
Date Thu, 09 Aug 2012 09:25:49 +0200
From Andreas Perstinger <andipersti@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Beautiful Soup Table Parsing
References <CAKfBN+eDYVx+Q1m8ee8PQqABtMDpZV07+KcopnNH9T-ETnEpUg@mail.gmail.com>
In-Reply-To <CAKfBN+eDYVx+Q1m8ee8PQqABtMDpZV07+KcopnNH9T-ETnEpUg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3095.1344497153.4697.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1344497153 news.xs4all.nl 6844 [2001:888:2000:d::a6]:38149
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26780

Show key headers only | View raw


On 09.08.2012 01:58, Tom Russell wrote:
> For instance this code below:
>
> soup = BeautifulSoup(urlopen('http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2.html?mod=mdc_pastcalendar'))
>
> table = soup.find("table",{"class": "mdcTable"})
> for row in table.findAll("tr"):
>      for cell in row.findAll("td"):
>          print cell.findAll(text=True)
>
> brings in a list that looks like this:

[snip]

> What I want to do is only be getting the data for NYSE and nothing
> else so I do not know if that's possible or not. Also I want to do
> something like:
>
> If cell.contents[0] == "Advances":
>      Advances = next cell or whatever??---> this part I am not sure how to do.
>
> Can someone help point me in the right direction to get the first data
> point for the Advances row? I have others I will get as well but
> figure once I understand how to do this I can do the rest.

To get the header row you could do something like:

header_row = table.find(lambda tag: tag.td.string == "NYSE")

 From there you can look for the next row you are interested in:

advances_row = header_row.findNextSibling(lambda tag: tag.td.string == 
"Advances")

You could also iterate through all next siblings of the header_row:

for row in header_row.findNextSiblings("tr"):
      # do something

Bye, Andreas

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


Thread

Re: Beautiful Soup Table Parsing Andreas Perstinger <andipersti@gmail.com> - 2012-08-09 09:25 +0200

csiph-web