【问题标题】:parse html beautiful soup解析html美汤
【发布时间】:2011-04-20 07:23:30
【问题描述】:

我有一个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/">

我需要一个解析电子邮件字符串

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

但它不起作用。 哪里错了?

【问题讨论】:

    标签: python regex beautifulsoup


    【解决方案1】:

    您的错误在于使用attrs dict 来查找电子邮件属性为空的元素。试试这个吧。

    #!/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"]
    

    打印具有email 属性的first a 元素的email 属性。如果您想要所有电子邮件,请尝试

    for link in soup.findAll("a", email=True):
        print link["email"]
    

    【讨论】:

    • 文件 "/usr/lib64/python2.6/site-packages/BeautifulSoup.py",第 599 行,在 getitem 中返回 self._getAttrMap()[key] KeyError : '电子邮件'
    • @user413036 根据您的实际测试数据再次更新。
    • @user413036 很高兴听到它,如果它适合你,请接受我的回答(点击我的回答旁边的复选框)
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2022-10-20
    • 2017-09-29
    • 2020-11-23
    • 1970-01-01
    • 2018-05-08
    相关资源
    最近更新 更多