[Tutor] using BeautifulSoup

jonasmg at softhome.net jonasmg at softhome.net
Tue Mar 28 11:02:53 CEST 2006


Kent Johnson writes: 

> jonasmg at softhome.net wrote:
>> anchor.findNext('code') fails:  
>> 
>> anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'})
>> print anchor  
>> 
>>  [<a href="/wiki/List_of_country_calling_codes" title="List of country
>> calling codes">Calling code</a>]  
>> 
>> 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


More information about the Tutor mailing list