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
2bin
V2EX  ›  Python

请教 Python 中 xpath 如何实现这种需求

  •  
  •   2bin · 2021-03-24 16:04:26 +08:00 · 1902 次点击
    这是一个创建于 1121 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下,我想获取 a 标签下的文本,aaabbbccc 作为列表一个值,而不是["aaa","bbb","ccc"],该如何处理呢?

    from lxml import etree
    
    html_str='''
    <span class="til">
        <a href="http://www.xxxx.com">
            "aaa"
            <br>
            "bbb"
            "ccc"
            <br>
        </a>
    </span>
    '''
    
    html = etree.HTML(html_str)
    content = html.xpath('//a/text()')
    print(content)
    
    """
    output:
    ['\n        "aaa"\n        ', '\n        "bbb"\n        "ccc"\n        ', '\n    ']
    """
    
    第 1 条附言  ·  2021-03-24 16:42:28 +08:00
    假如 span 下有多个 a 标签,设第二个 a 标签文本为"ddd","eee",输出结果会变成['aaa','bbb','ccc','ddd','eee'],能不能实现['aaabbbccc','dddeee']这样,就是说不管有多少个标签,相同标签下的文本合并一起
    12 条回复    2021-03-26 18:01:49 +08:00
    ch2
        1
    ch2  
       2021-03-24 16:14:32 +08:00
    改用 BeautifulSoup,取 node.text
    QuinceyWu
        2
    QuinceyWu  
       2021-03-24 16:28:28 +08:00
    price = [x.strip() for x in content if x.strip() != '']
    str1 = price[1].replace(" ", "").replace("\n", '').replace('"', "")
    str2 = price[0].replace('"', '')
    print(str2+str1)
    meiyoumingzi6
        3
    meiyoumingzi6  
       2021-03-24 16:32:24 +08:00
    列表都拿到了, 拼起来不就好了?
    mekingname
        4
    mekingname  
       2021-03-24 16:35:27 +08:00
    content = ''.join(x.strip() for x in html.xpath('//a/text()'))
    polarpy
        5
    polarpy  
       2021-03-24 16:41:29 +08:00
    拿出来的值替换换行跟空格
    mrleohe
        6
    mrleohe  
       2021-03-24 16:48:05 +08:00
    ''.join([i.strip() for i in ''.join(html.xpath('//a/text()')).split('"') ])
    CLCLCLCLCL
        7
    CLCLCLCLCL  
       2021-03-25 12:04:46 +08:00
    html = etree.HTML(html_str)
    content = html.xpath('string(//a)')

    直接用 string 就行
    2bin
        8
    2bin  
    OP
       2021-03-25 17:24:24 +08:00
    @CLCLCLCLCL 试了下,貌似只能提取第一个 a 标签的,有多个 a 后面不知道怎么提取出来
    zyb201314
        9
    zyb201314  
       2021-03-26 00:31:45 +08:00 via Android
    #这样?
    html = etree.HTML(html_str)
    lst=[]
    for a in html.xpath('//span//a'):
    content = a.xpath('.//text()')
    l=''.join("".join(content).split()).replace('"',"")
    lst.append(l)
    print(lst)
    CLCLCLCLCL
        10
    CLCLCLCLCL  
       2021-03-26 11:07:34 +08:00
    @2bin 是的, 循环一下 a 标签就行, 看你想用哪个了
    dongxiao
        11
    dongxiao  
       2021-03-26 15:36:17 +08:00
    html.xpath("string(//a)")
    2bin
        12
    2bin  
    OP
       2021-03-26 18:01:49 +08:00
    @zyb201314
    @CLCLCLCLCL
    谢谢两位,已经解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5135 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 01:18 · PVG 09:18 · LAX 18:18 · JFK 21:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.