推荐学习书目
› 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
cc7756789
V2EX  ›  Python

为什么爬虫获取的文本带有格式?

  •  
  •   cc7756789 · May 24, 2015 · 3011 views
    This topic created in 4144 days ago, the information mentioned may be changed or developed.
    其中 question.title() 是一个爬虫爬取html获得标题的结果。
    但是为什么自动有格式,而且我也没有发现带有换行符之类的, 甚至试过替换掉html标签,但也没有发现html标签。以前爬虫的学习仅限于普通的功能,忽略了这些问题,但是如今想实现更多的功能,不清楚为什么会出现这种情况。
    而且截取下来的标题,内容等统统都带有格式,搞得很多空行很不好看,可是没发现任何换行符之类的。
    ```
    a = os.path.split('/home/path/')
    if a[1] == '':
    a = a[0] + '/' + re.sub('<[^>]+>','',str(question.title())) + '.txt'
    print a
    ```

    结果
    ```
    /home/path/

    做寿司需要厨师哪方面的功夫?

    .txt
    ```
    questions.title()获取到的标题文本,中间自动带空行,如何让其变成普通的无格式字符串?
    变成 /home/path/做寿司需要厨师哪方面的功夫?.txt
    1 replies  •  2015-05-24 11:47:42 +08:00
    cc7756789
        1
    cc7756789  
    OP
       May 24, 2015
    额,原来是因为空格, 只需要用 s.strip()去掉空格就可以了。。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2655 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:29 · PVG 17:29 · LAX 02:29 · JFK 05:29
    ♥ Do have faith in what you're doing.