V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
maoyusu
V2EX  ›  Python

使用BeautifulSoup解析HTML时遇到了点儿问题...

  •  
  •   maoyusu · 2014-01-31 22:29:51 +08:00 · 3383 次点击
    这是一个创建于 3738 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如有一段HTML代码如下:

    <a href="/xiaoguo0131" title="西瓜小锅_" usercard="id=1744856272">西瓜小锅_</a><a href="http://club.weibo.com/intro" target="_blank"><i class="W_ico16 ico_club" node-type="daren" title="微博达人"></i></a><a href="http://hongbao.weibo.com/?ref=icon" target="_blank" title="让红包飞 "><i class="W_ico16 hongbaofei2014"></i></a>:192 <span class="S_txt2">(1月29日 22:33)</span>

    我想把那个':192'抓下来,怎么搞呢?他不再标签内部的说。・゚゚・(>д<;)・゚゚・。
    10 条回复    1970-01-01 08:00:00 +08:00
    binux
        1
    binux  
       2014-01-31 23:29:19 +08:00   ❤️ 1
    它在他的父节点(你没发出来的外层标签)的textnode中
    crazycookie
        2
    crazycookie  
       2014-01-31 23:41:12 +08:00   ❤️ 1
    感觉是在 不做好事啊 XD
    父节点,然后 x.text
    ooh
        3
    ooh  
       2014-02-01 00:35:24 +08:00   ❤️ 1
    r'</i></a>:([^<]+)'
    一般能用正则我不用其它
    maoyusu
        4
    maoyusu  
    OP
       2014-02-01 00:36:40 +08:00
    @crazycookie
    @binux
    你们真好,么么哒(´ε` )♡
    ruoyu0088
        5
    ruoyu0088  
       2014-02-01 06:38:06 +08:00   ❤️ 1
    stackoverflow上有个关于正则解析HTML的问题,回答都特别有意思:

    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
    maoyusu
        6
    maoyusu  
    OP
       2014-02-01 09:45:49 +08:00
    @ruoyu0088 233333333
    Ever
        7
    Ever  
       2014-02-01 09:53:56 +08:00   ❤️ 1
    soup.find('a', href="http://hongbao.weibo.com/?ref=icon", title='让红包飞 ').next_sibling
    maoyusu
        8
    maoyusu  
    OP
       2014-02-01 09:56:47 +08:00
    谢谢大家了,这个问题我已经处理完了.
    --------------------------------------------------------------------------------------------------
    韩三风:有没有183的妹子 (2013-12-29 13:37)
    花莲山水果冻大大王:178 湖南岳阳。 (2013-12-29 19:44)
    RSerizawa:170女,158男在哪里让我听见你的声音 (2013-12-29 17:13)
    长腿叔叔_ROY:183,哪都长你明白的吧 (2013-12-28 23:29)
    黄宇灯:170,有没有158的? (2013-12-29 23:29)
    后知后觉Klaus:160== (1月31日 23:23)
    Hinrichck:回复@取名无能滴_小绵:来吧 (1月30日 17:40)
    尼库塔丶喵呜呜:咱186吓死你~~//@正版丶嘟嘟猫: 好高!175+鞋!//@小蝶儿_我要看话剧QAQ:171cm~-w-//@趴在鼠标垫上的摄像头: 167 (1月30日 16:42)
    Elanne__chan:女158.3 (1月30日 14:10)
    分分钟涨姿势:179 (1月30日 12:35)
    berry璇:172 (1月30日 10:53)
    白菜菌:回复@RumPumRumPum: (1月30日 10:51)
    Clement_Chabernaud:妈呀,我不要找193的,我也不要169! (1月30日 10:45)
    安然大帝_:回复@_言子_:我157 我喜欢你@_言子_ (1月30日 09:57)
    Hinrichck:回复@进击的叶蘑菇: (1月30日 09:33)
    宸宸_陈:167 女 (1月30日 09:28)
    _言子_:我169 我喜欢你@安然大帝_ (1月30日 06:55)
    幸好你有我to静:哈哈163 (1月30日 01:58)
    madanIII:159.5啊。。。。。 (1月30日 00:57)
    进击的叶蘑菇:回复@BARRYMOT:看到的第一个180 163有资格跟你告白吗~ (1月30日 00:11)
    xx_叉妹:170女 (1月29日 23:59)
    LIQIYAO_:168 (1月29日 23:17)
    maoyusu
        9
    maoyusu  
    OP
       2014-02-01 09:57:51 +08:00
    哇擦,怎么还真能艾特到人啊(逃......
    maoyusu
        10
    maoyusu  
    OP
       2014-02-01 09:58:55 +08:00
    噢,窝想多了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:26 · PVG 13:26 · LAX 22:26 · JFK 01:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.