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
chaos0x0000007b
V2EX  ›  Python

Python 处理文件内容的正确姿势该怎样?

  •  
  •   chaos0x0000007b · 2017-03-21 10:37:29 +08:00 · 1467 次点击
    这是一个创建于 2807 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大神们:

    我想把 htm 文件中的第一个<link 到第二个<link 之间的所有内容另存为一个 htm 该怎么写比较简洁。



    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <link rel="prefetch" href="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

    <meta name="application-name" content="Python.org">
    <meta name="msapplication-tooltip" content="The official home of the Python Programming Language">
    <meta name="apple-mobile-web-app-title" content="Python.org">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="HandheldFriendly" content="True">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="cleartype" content="on">
    <meta http-equiv="imagetoolbar" content="false">

    <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"></script><script src="./Welcome to Python.org_files/modernizr.js.下载"></script><style type="text/css" adt="123"></style>

    <link href="./Welcome to Python.org_files/style.css" rel="stylesheet" type="text/css" title="default">
    <link href="./Welcome to Python.org_files/mq.css" rel="stylesheet" type="text/css" media="not print, braille, embossed, speech, tty">






    提取的内容应该是:



    <link rel="prefetch" href="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

    <meta name="application-name" content="Python.org">
    <meta name="msapplication-tooltip" content="The official home of the Python Programming Language">
    <meta name="apple-mobile-web-app-title" content="Python.org">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="HandheldFriendly" content="True">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="cleartype" content="on">
    <meta http-equiv="imagetoolbar" content="false">

    <script type="text/javascript" async="" src="https://ssl.google-analytics.com/ga.js"></script><script src="./Welcome to Python.org_files/modernizr.js.下载"></script><style type="text/css" adt="123"></style>

    <link
    2 条回复    2017-03-21 10:50:23 +08:00
    Kilerd
        1
    Kilerd  
       2017-03-21 10:44:03 +08:00
    xpath or regex
    cdwyd
        2
    cdwyd  
       2017-03-21 10:50:23 +08:00 via Android
    '<link' + html.split('<link')[1]
    手机打的没测试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1858 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:32 · PVG 00:32 · LAX 08:32 · JFK 11:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.