V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
ihciah
0.2D
0.03D
V2EX  ›  Google App Engine

Python如何截取特定字符串?

  •  
  •   ihciah · 2012 年 6 月 14 日 · 5463 次点击
    这是一个创建于 4973 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例子:
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <p>输出我</p>
    <p>我来捣乱</p>
    </body>
    </html>

    用Pyhton提取其中的'输出我'。输出第一个<p>和第一个</p>之间的内容。

    谢谢!!
    13 条回复    1970-01-01 08:00:00 +08:00
    INT21H
        1
    INT21H  
       2012 年 6 月 14 日   ❤️ 1
    >>> from BeautifulSoup import BeautifulSoup
    >>> html="""<html>
    ... <head>
    ... <title>Test</title>
    ... </head>
    ... <body>
    ... <p>输出我</p>
    ... <p>我来捣乱</p>
    ... </body>
    ... </html>"""
    >>> bs = BeautifulSoup(html)
    >>> bs.p
    <p>输出我</p>
    >>> bs.p.contents
    [u'\u8f93\u51fa\u6211']
    >>>
    vfasky
        2
    vfasky  
       2012 年 6 月 14 日
    <code>
    html = '''<html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <p>输出我</p>
    <p>我来捣乱</p>
    </body>
    </html>'''

    for t in html.split('</p>') :
    print t.replace('<p>','')
    break;
    </code>
    vfasky
        3
    vfasky  
       2012 年 6 月 14 日   ❤️ 1
    muzuiget
        4
    muzuiget  
       2012 年 6 月 14 日
    关键词:正则表达式,DOM。
    goofansu
        5
    goofansu  
       2012 年 6 月 14 日
    最近也在玩,beautifulsoup很棒
    yibin001
        6
    yibin001  
       2012 年 6 月 14 日
    beautifulsoup还真是个神器
    likuku
        7
    likuku  
       2012 年 6 月 14 日   ❤️ 1
    #!/usr/bin/env python
    # encoding: utf-8
    """
    html.py

    Created by likuku on 2012-06-14.
    Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    """

    import sys
    import os

    html="""
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <p>输出我</p>
    <p>我来捣乱</p>
    </body>
    </html>
    """


    def main():
    for text in html.split('\n'):
    if text.find('<p>') != -1:
    tmp = text.replace('</p>','').replace('<p>','')
    print tmp
    break

    if __name__ == '__main__':
    main()
    aa88kk
        8
    aa88kk  
       2012 年 6 月 14 日   ❤️ 1
    用正则:
    m = re.search('<p>(.*?)<\/p>', s, re.S)
    cute
        9
    cute  
       2012 年 6 月 14 日   ❤️ 1
    start = s.find('<p>')+ len('<p>')
    end = s.find('</p>', start)
    print s[start:end]
    ihciah
        10
    ihciah  
    OP
       2012 年 6 月 14 日
    谢谢各位!!~~~~~~~~·
    ling0322
        11
    ling0322  
       2012 年 6 月 18 日
    其实有一个比beautifulsoap更霸气的, 叫pyQuery
    binux
        12
    binux  
       2012 年 6 月 18 日
    beautifulsoup太费内存了
    chairo
        13
    chairo  
       2012 年 6 月 18 日
    libxml路过
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   5253 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:28 · PVG 17:28 · LAX 01:28 · JFK 04:28
    ♥ Do have faith in what you're doing.