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

python 下 lxml 解析 html 问题

  •  
  •   wico77 · 2015-06-15 16:43:25 +08:00 · 2706 次点击
    这是一个创建于 3249 天前的主题,其中的信息可能已经有所发展或是发生改变。

    s= requests.get(url)
    d = html.fromstring(s.text)
    q = d.xpath('//td[@class="postcell"]/div/div[@class="post-text"]/*')
    for i in q:
    print i
    结果:
    <Element p at 0x10ae26f70>
    <Element p at 0x10ae26fc8>
    <Element pre at 0x10ae32050>
    <Element p at 0x10ae320a8>
    <Element p at 0x10ae32100>
    <Element p at 0x10ae32158>
    <Element p at 0x10ae321b0>
    <Element p at 0x10ae32208>
    请问如何让段落信息显示出来?

    4 条回复    2015-06-15 17:17:18 +08:00
    JasperYanky
        1
    JasperYanky  
       2015-06-15 16:50:08 +08:00
    /text()
    wico77
        2
    wico77  
    OP
       2015-06-15 16:56:12 +08:00
    @JasperYanky 用text()的话就得到里面的文字,我想把成段的内容都得到。包括<p>, </p>
    blueset
        3
    blueset  
       2015-06-15 17:14:55 +08:00 via Android
    etree.tostring(i)
    wico77
        4
    wico77  
    OP
       2015-06-15 17:17:18 +08:00
    @blueset 问题解决。谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1520 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.