Re: [Tutor] using BeautifulSoup

2006-03-28 Thread jonasmg
Kent Johnson writes: 

> [EMAIL PROTECTED] wrote:
>> anchor.findNext('code') fails:  
>> 
>> anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
>> print anchor  
>> 
>>  [Calling code]  
>> 
>> anchor.findNext('code')
>> [] 
> 
> are you sure that's what you got? Looks like an AttributeError to me - 
> anchor is a *list* of anchors. Try
> anchor[0].findNext('code') 
> 
> Kent 
> 

With 'fetch' you get a list of Tag objects, so there is that using: 

anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
anchor[0].findNext('code') 

But with 'findChild' or 'first' you get only the first Tag that matches, so: 

anchor = soup.findChild('a', {'href': 
'/wiki/List_of_country_calling_codes'})
anchor.findNext('code') 

Thanks for your help, Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using BeautifulSoup

2006-03-27 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> anchor.findNext('code') fails: 
> 
> anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
> print anchor 
> 
>  [Calling code] 
> 
> anchor.findNext('code')
> [] 

are you sure that's what you got? Looks like an AttributeError to me - 
anchor is a *list* of anchors. Try
anchor[0].findNext('code')

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using BeautifulSoup

2006-03-27 Thread jonasmg
[EMAIL PROTECTED] writes: 

>>jonasmg at softhome.net wrote:
>>> Hi!
>>> 
>>> I'm trying to use BeautifulSoup for get data from a table (on right) from:
>>> http://en.wikipedia.org/wiki/United_states
>>> 
>>> i.e. i would get data from 'Calling code' that it would be '+1'
>>> 
>>>  --
>>> 
>>> import urllib2
>>> from BeautifulSoup import BeautifulSoup
>>> 
>>> url="http://en.wikipedia.org/wiki/United_states";
>>> html = urllib2.urlopen(url).read()
>>> soup = BeautifulSoup()
>>> soup.feed(html) 
> 
>> You just have to find some kind of ad hoc search that gets you to where 
>> you want to be. I would try something like this:
> 
>> anchor = soup.fetch('a', dict(href="/wiki/List_of_country_calling_codes"))
> 
>> code = anchor.findNext('code')
>> print code.string
> 
>> Presumably you want this to work for other country pages as well; you 
>> will have to look at the source, see what they have in common and search 
>> on that.
> 
>> Kent
> 
> anchor.findNext('code') fails:  
> 
> anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
> print anchor  
> 
>  [Calling code]  
> 
> anchor.findNext('code')
> []  
> 
> P.S. : Sorry for my last email, I was wrong with the subject
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Solution _there is that using findChild instead of fetch_: 

anchor = soup.findChild('a', 
dict(href="/wiki/List_of_country_calling_codes")) 

print anchor.findNext('code') 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using BeautifulSoup

2006-03-27 Thread jonasmg
>jonasmg at softhome.net wrote:
>> Hi!   
>> 
>> I'm trying to use BeautifulSoup for get data from a table (on right) from:
>> http://en.wikipedia.org/wiki/United_states   
>> 
>> i.e. i would get data from 'Calling code' that it would be '+1'   
>> 
>>  --   
>> 
>> import urllib2
>> from BeautifulSoup import BeautifulSoup   
>> 
>> url="http://en.wikipedia.org/wiki/United_states";
>> html = urllib2.urlopen(url).read()
>> soup = BeautifulSoup()
>> soup.feed(html) 

> You just have to find some kind of ad hoc search that gets you to where 
> you want to be. I would try something like this:

> anchor = soup.fetch('a', dict(href="/wiki/List_of_country_calling_codes"))

> code = anchor.findNext('code')
> print code.string

> Presumably you want this to work for other country pages as well; you 
> will have to look at the source, see what they have in common and search 
> on that.

> Kent

anchor.findNext('code') fails: 

anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
print anchor 

 [Calling code] 

anchor.findNext('code')
[] 

P.S. : Sorry for my last email, I was wrong with the subject
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using BeautifulSoup

2006-03-27 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> Hi! 
> 
> I'm trying to use BeautifulSoup for get data from a table (on right) from:
> http://en.wikipedia.org/wiki/United_states 
> 
> i.e. i would get data from 'Calling code' that it would be '+1' 
> 
>  -- 
> 
> import urllib2
> from BeautifulSoup import BeautifulSoup 
> 
> url="http://en.wikipedia.org/wiki/United_states";
> html = urllib2.urlopen(url).read()
> soup = BeautifulSoup()
> soup.feed(html) 

You just have to find some kind of ad hoc search that gets you to where 
you want to be. I would try something like this:

anchor = soup.fetch('a', dict(href="/wiki/List_of_country_calling_codes"))

code = anchor.findNext('code')
print code.string

Presumably you want this to work for other country pages as well; you 
will have to look at the source, see what they have in common and search 
on that.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using BeautifulSoup

2006-03-27 Thread jonasmg
Hi! 

I'm trying to use BeautifulSoup for get data from a table (on right) from:
http://en.wikipedia.org/wiki/United_states 

i.e. i would get data from 'Calling code' that it would be '+1' 

 -- 

import urllib2
from BeautifulSoup import BeautifulSoup 

url="http://en.wikipedia.org/wiki/United_states";
html = urllib2.urlopen(url).read()
soup = BeautifulSoup()
soup.feed(html) 

mainTable = soup.first('table')
rows = mainTable('tr') 


any help here? 

Thanks in advance 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor