Python如何截取特定字符串? - V2EX
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
ihciah
0.2D
0.03D

Python如何截取特定字符串?

  •  
  •   ihciah Jun 14, 2012 5618 views
    This topic created in 5082 days ago, the information mentioned may be changed or developed.
    例子:
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <p>输出我</p>
    <p>我来捣乱</p>
    </body>
    </html>

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

    谢谢!!
    13 replies    1970-01-01 08:00:00 +08:00
    INT21H
        1
    INT21H  
       Jun 14, 2012   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  
       Jun 14, 2012
    <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  
       Jun 14, 2012   1
    muzuiget
        4
    muzuiget  
       Jun 14, 2012
    关键词:正则表达式,DOM。
    goofansu
        5
    goofansu  
       Jun 14, 2012
    最近也在玩,beautifulsoup很棒
    yibin001
        6
    yibin001  
       Jun 14, 2012
    beautifulsoup还真是个神器
    likuku
        7
    likuku  
       Jun 14, 2012   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  
       Jun 14, 2012   1
    用正则:
    m = re.search('<p>(.*?)<\/p>', s, re.S)
    cute
        9
    cute  
       Jun 14, 2012   1
    start = s.find('<p>')+ len('<p>')
    end = s.find('</p>', start)
    print s[start:end]
    ihciah
        10
    ihciah  
    OP
       Jun 14, 2012
    谢谢各位!!~~~~~~~~
    ling0322
        11
    ling0322  
       Jun 18, 2012
    其实有一个比beautifulsoap更霸气的, 叫pyQuery
    binux
        12
    binux  
       Jun 18, 2012
    beautifulsoup太费内存了
    chairo
        13
    chairo  
       Jun 18, 2012
    libxml路过
    About     Help     Advertise     Blog     API     FAQ     Solana     5566 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 70ms UTC 08:45 PVG 16:45 LAX 01:45 JFK 04:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86