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

bs4 爬虫处理 html 标签的问题,关于带空格的属性标签

  •  
  •   pppguest3962 · Aug 20, 2018 · 3257 views
    This topic created in 2967 days ago, the information mentioned may be changed or developed.
    <td class="stream-6771">
        <a href="javascript:void(0);" id="vid_667" class="vh"><code style="display:none">已发送</code><i>7621</i></a>
        <s></s>
    </td>
    
    dataSoup = BeautifulSoup(tr.prettify(),'lxml')
    tmpStr = dataSoup.find_all("code", class_= ["code","style"])
    

    查阅文档和看了百度别人的经验,理论上这么做是能获取到“已发送”的字符串,

    是否跟 python 的版本有关系? 用的是 python3,文档似乎更新时间久了,python2 的写法不能用?

    谢谢解答!

    4 replies  •  2018-08-20 21:21:54 +08:00
    epicnoob
        1
    epicnoob  
       Aug 20, 2018
    dataSoup.find_all("code")
    或
    dataSoup.find_all(attrs={'style': 'display:none'})
    pppguest3962
        2
    pppguest3962  
    OP
       Aug 20, 2018
    @epicnoob,感谢您的回复解答,find_all("code")可以直接取出
    ```
    <code style="display:none">已发送</code>
    ```

    这个方法小喵是是摸索出来了,

    其实帖里问的是如何直接取出“已发送”这三个中文字作为 tmpStr。。。

    其实还是想知道如果换了方式,我还能明白技巧在哪里。。。

    ```
    <code class="display:none">已发送变化方式之一</code>
    <class fire="display:none">已发送变化方式之二</class>
    <em class="display:none">已发送变化方式之三</em>
    ```

    尽管不一定会有这样的 HTML 代码,只是想了解一下这种空格的标签,如何直接套取 text()。。。。。

    感激不尽~~~!!!
    zzj0311
        3
    zzj0311  
       Aug 20, 2018 via Android
    .text。。
    Xs0ul
        4
    Xs0ul  
       Aug 20, 2018
    dataSoup.find('code', attrs={'style':'display:none'}).text
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2283 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:25 · PVG 13:25 · LAX 22:25 · JFK 01:25
    ♥ Do have faith in what you're doing.