python - bs爬下div后如何获得最外层标签?
大家讲道理
大家讲道理 2017-04-18 10:03:34
[Python讨论组]
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
PHPz
    <p class="l_post l_post_bright j_l_post clearfix " data-field='{"author":{"user_id":348570172,   "user_name":"\u6446\u6446\u821e\u66f2","props":null},"content":{"post_id":31489927386,"is_anonym":false,"forum_id":874949,"thread_id":2108034524,"content":"912904081@qq.com\u8c22\u8c22\u6492","post_no":94,"type":"0","comment_num":0,"props":null,"post_index":0,"pb_tpoint":null}}'> <p class="d_author"> <ul class="p_author">
    ...
    </p>

要爬取的是这个p最外层的标签里user_name和content,中间还有好多好多标签,就是把这个p里的都爬下来了,想知道怎么就留最外面我需要的这个

天蓬老师
   r = requests.get("http://tieba.baidu.com/p/2108034524?pn=4")
   soup = BeautifulSoup(r.content, "lxml")
   users = soup.find_all("p", class_="l_post")
   for user in users:
       print(user["data-field"])
       # 其他处理

然后对取出的内容再进行处理

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号