473,386 Members | 1,799 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,386 software developers and data experts.

how to find not the next sibling but the 2nd sibling or find sibling "a" OR sinbling "b"

i have some html which looks like this where i want to scrape out the
href stuff (the www.cnn.com part)

<div class="noFood">Cheese</div>
<div class="food">Blue</div>
<a class="btn" href = "http://www.cnn.com">
so i wrote this code which scrapes it perfectly:

for incident in row('div', {'class':'noFood'}):
b = incident.findNextSibling('div', {'class': 'food'})
print b
n = b.findNextSibling('a', {'class': 'btn'})
print n
link = n['href'] + "','"

problem is that sometimes the 2nd tag , the <div class="food"> tag , is
sometimes called food, sometimes called drink. so sometimes it looks
like this:

<div class="noFood">Cheese</div>
<div class="drink">Pepsi</div>
<a class="btn" href = "http://www.cnn.com">

how do i alter my script to take into account the fact that i will
sometimes have food and sometimes have drink as the class name? is
there a way to say "look for food or drink" or a way to say "look for
this incident and then find not the next sibling but the 2nd next
sibling" if that makes any sense?

thanks

Jan 18 '06 #1
11 2111
lo************@gmail.com wrote:
i have some html which looks like this where i want to scrape out the
href stuff (the www.cnn.com part)

<div class="noFood">Cheese</div>
<div class="food">Blue</div>
<a class="btn" href = "http://www.cnn.com">
so i wrote this code which scrapes it perfectly:

for incident in row('div', {'class':'noFood'}):
b = incident.findNextSibling('div', {'class': 'food'})
print b
n = b.findNextSibling('a', {'class': 'btn'})
print n
link = n['href'] + "','"

problem is that sometimes the 2nd tag , the <div class="food"> tag , is
sometimes called food, sometimes called drink.


Apparently you are using Beautiful Soup. The value in the attribute
dictionary can be a callable; try this:

def isFoodOrDrink(attr):
return attr in ['food', 'drink']

b = incident.findNextSibling('div', {'class': isFoodOrDrink})

Alternately you could omit the class spec and check for it in code.

Kent
Jan 19 '06 #2
i actually realized there are 3 potentials for class names. either
food or drink or dessert. so my question is whether or not i can alter
your function to look like this?

def isFoodOrDrinkOrDesert(attr):
return attr in ['food', 'drink', 'desert']
thanks in advance for the help

Kent Johnson wrote:
lo************@gmail.com wrote:
i have some html which looks like this where i want to scrape out the
href stuff (the www.cnn.com part)

<div class="noFood">Cheese</div>
<div class="food">Blue</div>
<a class="btn" href = "http://www.cnn.com">
so i wrote this code which scrapes it perfectly:

for incident in row('div', {'class':'noFood'}):
b = incident.findNextSibling('div', {'class': 'food'})
print b
n = b.findNextSibling('a', {'class': 'btn'})
print n
link = n['href'] + "','"

problem is that sometimes the 2nd tag , the <div class="food"> tag , is
sometimes called food, sometimes called drink.


Apparently you are using Beautiful Soup. The value in the attribute
dictionary can be a callable; try this:

def isFoodOrDrink(attr):
return attr in ['food', 'drink']

b = incident.findNextSibling('div', {'class': isFoodOrDrink})

Alternately you could omit the class spec and check for it in code.

Kent


Jan 19 '06 #3
lo************@gmail.com wrote:
i actually realized there are 3 potentials for class names. either
food or drink or dessert. so my question is whether or not i can alter
your function to look like this?

def isFoodOrDrinkOrDesert(attr):
return attr in ['food', 'drink', 'desert']


what happens when you try that ?

</F>

Jan 19 '06 #4
lo************@gmail.com wrote:
i actually realized there are 3 potentials for class names. either
food or drink or dessert. so my question is whether or not i can alter
your function to look like this?

def isFoodOrDrinkOrDesert(attr):
return attr in ['food', 'drink', 'desert']


Check the spelling of 'dessert' and give it a try.

Kent
Jan 19 '06 #5
ok i found something that works. instead of using the def i did this:

for incident in row('div', {'class': 'food' or 'drink' }):

and it worked!

only thing is that i think i am messing up the logic and here is why

So when i run my script i get results, meaning it scrapes some stuff
out,
but then i get errors and where i am told:

TypeError: unsupported operand type(s) for +: 'NullType' and 'str'
Is this because of the logic in my code? i mean what i want the script
to
do is look for the <Tr> tag and then find the first div tag named food
or
drink, find its sibling named food, drink or dessert and then find the
button tag which is the following sibling and THEN scrape out the href.
is
it possible that after it finds that first div and looks for the next
sibling and then the next siblings href that it then tries to run the
same
process all over again starting with the 2nd div tag and being that it
can't
find the another div tag after the 2nd div tag that it trips up?

know what i mean?
here is my code:

for row in bs('tr'):
for incident in row('div', {'class': 'food' or 'drink'}):
b = incident.findNextSibling('div', {'class': 'food' or 'drink'
or
'dessert'})
n = b.findNextSibling('a', {'class': 'btn'})
link= n['href'] + "','"

Jan 19 '06 #6
ho***********@gmail.com wrote:
ok i found something that works. instead of using the def i did this:

for incident in row('div', {'class': 'food' or 'drink' }):

and it worked!


'food' or 'drink' doesn't do what you think it does:
'food' or 'drink' 'food'
{'class': 'food' or 'drink'}

{'class': 'food'}

</F>

Jan 19 '06 #7
hey fredrik,

i don't understand what you are saying

Fredrik Lundh wrote:
ho***********@gmail.com wrote:
ok i found something that works. instead of using the def i did this:

for incident in row('div', {'class': 'food' or 'drink' }):

and it worked!


'food' or 'drink' doesn't do what you think it does:
>>> 'food' or 'drink' 'food'
>>> {'class': 'food' or 'drink'}

{'class': 'food'}

</F>


Jan 19 '06 #8
lo************@gmail.com wrote:
hey fredrik,

i don't understand what you are saying
Do what he showed in the Python interactive shell,
Fredrik Lundh wrote:
'food' or 'drink' doesn't do what you think it does:
>>> 'food' or 'drink'

'food'
>>> {'class': 'food' or 'drink'}

{'class': 'food'}


"or" returns the first true element, anything but False or None, I
think... so 'food' (a string) is true, and always will return in that
code.

http://diveintopython.org/power_of_i...on/and_or.html

Brett

Jan 20 '06 #9
Brett Hoerner wrote:
lo************@gmail.com wrote: [...] "or" returns the first true element, anything but False or None, I
think... so 'food' (a string) is true, and always will return in that
code.


Just in case newbies are reading: in Python several different values are
considered false in the context of an "if" statement. These include

False # Boolean False
0 # The integer zero
0.0 # Floating-point zero
(0+0j) # Complex zero
None # The None object
[] # The empty list
() # The empty tuple
{} # The empty dictionary

This is mainly to allow the convenience of writing

if thing:
...

However, one has to be careful that code that needs to treat None
differently from [] uses explicit testing such as

if thing is None:
...

You can also construct your own classes so their instances evaluate to
True or False according to your needs.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 20 '06 #10
well actually all i want it to do is find the first thing that shows up
whether its class:food or class: drink so that works for me. only
thing is that after it finds class:food i think it runs through the
html again and finds the following class:drink and being that there is
not class tag after that class: drink tag it fails.

Fredrik Lundh wrote:
ho***********@gmail.com wrote:
ok i found something that works. instead of using the def i did this:

for incident in row('div', {'class': 'food' or 'drink' }):

and it worked!


'food' or 'drink' doesn't do what you think it does:
>>> 'food' or 'drink' 'food'
>>> {'class': 'food' or 'drink'}

{'class': 'food'}

</F>


Jan 23 '06 #11
lo************@gmail.com wrote:
well actually all i want it to do is find the first thing that shows up
whether its class:food or class: drink so that works for me.


what makes you think that looking "food" only will find either "food"
or "drink" ?

</F>

Jan 23 '06 #12

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

Similar topics

3
by: Peter Rohleder | last post by:
Hi, I'm using a style-sheet where I make use of the XPATH-"following-sibling"-expression. The part which makes problems looks similar to the following code: --------------------------- ...
2
by: John Smith | last post by:
I have more than 4,000 pieces of email in my Yahoo account. I would like to write a program to download them all. In order to write such a program, I need to know a few things. 1. After I fill...
3
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be...
3
by: Rudy Ray Moore | last post by:
Hi guys, I often use the "Find" (aka CTRL-F) tool in "Visual Studio .net 2003 7.1 c++". I noticed that that dialog box does not go away when I perform a search (it stays on the screen). This...
2
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP...
2
by: ntsNews | last post by:
Hi, Using the built in command "Find Next" is there a way to have the Match Drop Down Menu to default to "Start of Field"... or can it be the only option? GCM
1
by: Sion Arrowsmith | last post by:
I have a module which needs to know what directory it's in, and to refer to files in a sibling directory, something like App/src/foo.py wants to read App/data/conf.xml . But I have no idea in what...
3
by: apurvaG | last post by:
Hi All, I am facing a problem while fetching a preceding sibling with some specific name from current node. Following is the sample XML which I am using <Root> <document> ...
3
by: =?ISO-8859-1?Q?Andreas_M=FCller?= | last post by:
Hi! (Python 2.2.3 if this is relevant :-) I have a list of objects with, lets say, the attributes "ID", "x" and "y". Now I want to find the index of list element with ID=10. Of course I can...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.