Python: BeautifulSoup extract text from anchor tag
Question: I want to extract text from following src of the image tag and text of the
anchor tag which is inside the div class data.

I successfully manage to extract the img src but I am having trouble on
extracting the text from the anchor tag.

    
    
    <a class="title" href="http://rads.stackoverflow.com/amzn/click/B0073HSK0K">Nikon COOLPIX L26 16.1 MP Digital Camera with 5x Zoom NIKKOR Glass Lens and 3-inch LCD (Red)</a> 
    

Here is the link for the entire [HTML
page](http://www.amazon.com/s/ref=sr_pg_1?rh=n:172282,k%3adigital%20camera&keywords=digital%20camera&ie=UTF8&qid=1343600585)

Here is my code

    
    
    for div in soup.findAll('div', attrs={'class':'image'}):
        print "\n"
        for data in div.findNextSibling('div', attrs={'class':'data'}):
            for a in data.findAll('a', attrs={'class':'title'}):
                print a.text
        for img in div.findAll('img'):
            print img['src']
    

What I am trying to do is extract the image src (link) and the title in side
the div class=data.

so for example

    
    
     <a class="title" href="http://rads.stackoverflow.com/amzn/click/B0073HSK0K">Nikon COOLPIX L26 16.1 MP Digital Camera with 5x Zoom NIKKOR Glass Lens and 3-inch LCD (Red)</a> 
    

I want to extract : `Nikon COOLPIX L26 16.1 MP Digital Camera with 5x Zoom
NIKKOR Glass Lens and 3-inch LCD (Red)`


Answer: This will help:

    
    
    from BeautifulSoup import BeautifulSoup
    
    data = '''<div class="image">
            <a href="http://www.example.com/eg1">Content1<img  
            src="http://image.example.com/img1.jpg" /></a>
            </div>
            <div class="image">
            <a href="http://www.example.com/eg2">Content2<img  
            src="http://image.example.com/img2.jpg" /> </a>
            </div>'''
    
    soup = BeautifulSoup(data)
    
    for div in soup.findAll('div', attrs={'class':'image'}):
        print div.find('a')['href']
        print div.find('a').contents[0]
        print div.find('img')['src']
    

If you are looking into Amazon products then you should be using the official
API. There is at least [one Python
package](http://pypi.python.org/pypi/python-amazon-product-api/) that will
ease your scraping issues and keep your activity within the terms of use.

