解析HTML美丽的汤 [英] parse html beautiful soup

查看:156
本文介绍了解析HTML美丽的汤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML页面

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

我neeed解析电子邮件字符串

I neeed a parse email string

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

但它不工作。
错误在哪里?

But it not working. Where mistake?

推荐答案

您的错误是在使用 ATTRS 字典查找与空的邮件属性的元素。试试这个吧。

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"]

这篇关于解析HTML美丽的汤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆