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

求助 用 xpath 如何取到 title 里的文字啊

  •  
  •   F1024 · 2017-12-30 00:10:15 +08:00 · 4496 次点击
    这是一个创建于 2280 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <div class="item-pic">
    <a href="//2.taobao.com/item.htm?id=560088094729" target="_blank" title=" [转卖] 创得 小米 5 手机壳小米 5s 保护套小米 6 防摔硅</a>
    </div>

    大佬们用 xpath 如何取到 title 里的文字啊?

    import os
    import requests
    from lxml import etree

    res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=小米 6&st_edtime=1').content
    txt = etree.HTML(res)
    txt2 = txt.xpath('//div/a/text()')

    for tt in txt2:
    print(tt)

    os.system("pause")
    这样取不到
    dd0754
        1
    dd0754  
       2017-12-30 01:27:47 +08:00   ❤️ 1
    ```python
    import os
    import requests
    from lxml import etree

    res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=小米 6&st_edtime=1').content
    txt = etree.HTML(res)
    items = txt.xpath('//div[@class="ks-waterfall"]')
    for item in items[1:]:
    print(item.attrib)
    a = item.xpath('./div/div[@class="item-info"]/div/a')
    for d in a:
    print(d.attrib)

    ```
    F1024
        2
    F1024  
    OP
       2017-12-30 10:44:10 +08:00
    @dd0754
    import requests
    from lxml import etree

    res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=ddr3 1866&st_edtime=1').content
    txt = etree.HTML(res)

    txt4 = txt.xpath('//div/div[@class="item-info"]/div/a')

    print(txt4[0].attrib.get('title'))
    可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3942 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:22 · PVG 18:22 · LAX 03:22 · JFK 06:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.