views:

87

answers:

1

Hi all. I have a html page

<a email="[email protected]" href="http://www.max.ru/agent?message&amp;[email protected]" title="Click herе" class="mf_spIco spr-mrim-9"></a><a class="mf_t11" type="booster" href="http://max.ru/mail/corporate/"&gt;

I neeed a parse email string

    soup = BeautifulSoup(data
    string = soup.find("a",{"email": ""})
    print string

But it not working. Where mistake?

A: 

Your mistake was in using the attrs dict to look for elements with an email attribute that is empty. Try this instead.

#!/usr/bin/env python

from BeautifulSoup import BeautifulSoup
import urllib2

req = urllib2.urlopen('http://worldnuclearwar.ru')

soup = BeautifulSoup(req)
print soup.find("a", email=True)["email"]

To print the email attribute of the first a element which has an email attribute. If you want all emails, try

for link in soup.findAll("a", email=True):
    print link["email"]
Day
Already try it.
File "/usr/lib64/python2.6/site-packages/BeautifulSoup.py", line 599, in __getitem__ return self._getAttrMap()[key]KeyError: 'email'
Edited to show complete script that works for me
Day
yes it working but when it in a real script not.
@user413036 Updated again given your actual test data.
Day
Thanks. all works fine
@user413036 Glad to hear it, please accept my answer if it works for you (click the tick box next to my answer)
Day