473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

python/xpath question..

morning....

i apologize up front as this is really more of an xpath question..

in my python, i'm using the xpath function to iterate/parse some html. i can
do something like

s=d.xpath("//tr/td/text()")
count=len(s)

and get the number of nodes that have text

i can then do something like
s=d.xpath("//tr/td")
count2=len(s)

and get the number of total nodes...
by subtracting, i can get the number of nodes, without text.. is there an
easier way??!!
count2-count

ie, if i have something like
<tr>
<td></td>
<td>foo</td>
</tr>

is there a way to get the count that there is a single "td" node with
text()=""

thanks
Sep 3 '08 #1
2 1350
Hi,

you should really read about XPath. There are also newsgroups specifically for
this topic, please use them.
bruce wrote:
in my python, i'm using the xpath function to iterate/parse some html. i can
do something like

s=d.xpath("//tr/td/text()")
count=len(s)

and get the number of nodes that have text
That is a wrong assumption. It will give you the number of text nodes, not the
number of elements. They may or may not be the same.

i can then do something like
s=d.xpath("//tr/td")
count2=len(s)

and get the number of total nodes...
by subtracting, i can get the number of nodes, without text.. is there an
easier way??!!
Yes, learn to use XPath, e.g.

//tr/td[not string()]

Stefan
Sep 4 '08 #2
Stefan Behnel wrote:
Yes, learn to use XPath, e.g.

//tr/td[not string()]
Oh, well...

//tr/td[not(string())]

as I said, wrong news group. ;-)

Try something like "gmane.text.xml.xpath.general", for example.

Stefan
Sep 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nelson Minar | last post by:
Could someone help me get started using XPath or XQuery in Python? I'm overwhelmed by all the various options and am lacking guidance on what the simplest way to go is. What library do I need to...
3
by: Thierry Lam | last post by:
Let's say I have the following xml tag: <para role="success">1</para> I can't figure out what kind of python xml.dom codes I should invoke to read the data 1? Any help please? Thanks...
5
by: And80 | last post by:
Hi, I would like to use xpath modules in python2.4.... In my local machine I am running python2.3.5 and on the server I run python2.4. I have seen that while on my computer i am able to import...
0
by: bruce | last post by:
hi paul... in playing around with the test python app (see below) i've got a couple of basic questions. i can't seem to find the answers via google, and when i've looked in the libxml2dom stuff...
3
by: bruce | last post by:
for guys with python/xpath expertise.. i'm playing with xpath.. and i'm trying to solve an issue... i have the following kind of situation where i'm trying to get certain data. i have a...
1
by: bruce | last post by:
Hi Paul... Thanks for the reply. Came to the same conclusion a few minutes before I saw your email. Another question: tr=d.xpath(foo) gets me an array of nodes.
1
by: bruce | last post by:
Hi. Got a test web page, that basically has two "<html" tags in it. Examining the page via Firefox/Dom Inspector, I can create a test xpath query "/html/body/form" which gets the target form for...
1
by: bruce | last post by:
hi... i've got the following situation, with the following test url: "http://schedule.psu.edu/soc/fall/Alloz/a-c/acctg.html#". i can generate a list of the tables i want for the courses on the...
0
by: John Krukoff | last post by:
On Wed, 2008-09-03 at 13:36 -0700, bruce wrote: Well, you could just do the test (and the count!) in the xpath expression: count( //tr/td ) It sounds like you're not familiar with xpath? I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.