爬虫:BeautifulSoup(三)

版权声明:欢迎转载,注明出处 https://blog.csdn.net/jklcl/article/details/81591073

今天在和大佬聊天的时候,大佬们谈论到最近在知乎上一篇数据分析的文章,关于女性文胸的统计和分析,“用Python抓取某东购买记录并统计MM的bra大小”,虽然没有使用到BeautifulSoup,不过也需要学习一下,嘻嘻。
知乎网站:https://zhuanlan.zhihu.com/p/40487715

这里主要是tag修饰的字符串和注释部分提取

# coding=utf-8

import requests
from bs4 import BeautifulSoup
#字符串常被包含在tag内.Beautiful Soup用 NavigableString 类来包装tag中的字符串:
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>',features="html.parser")
tag = soup.b
print (tag.string)
print(type(tag.string))

#注释及特殊字符串
#Tag , NavigableString , BeautifulSoup 几乎覆盖了html和xml中的所有内容,
# 但是还有一些特殊对象.容易让人担心的内容是文档的注释部分
markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup = BeautifulSoup(markup ,features="html.parser")
comment = soup.b.string
print (type(comment))

#Comment 对象是一个特殊类型的 NavigableString 对象:
print(comment)

#但是当它出现在HTML文档中时, Comment 对象会使用特殊的格式输出:
print(soup.b.prettify())

猜你喜欢

转载自blog.csdn.net/jklcl/article/details/81591073