个人拙见
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/70ebbe9b76320839524509b3259e9286.png)
import requests
from bs4 import BeautifulSoup
url="https://blog.csdn.net/Gao__xi/article/details/88604425"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126",
"accept-language":"zh-CN,zh;q=0.8",
"cookie":"TINGYUN_DATA=%7B%22id%22%3A%22-sf2Cni530g%23HL5wvli0FZI%22%2C%22n%22%3A%22WebAction%2FCI%2Farticle%252Fdetails%22%2C%22tid%22%3A%22619af69fc20d4e%22%2C%22q%22%3A0%2C%22a%22%3A186%7D; __yadk_uid=7dMyMKSPeeUR9roO4TQ7bHQUnd7Oyzx5; uuid_tt_dd=10_28867322950-1540695012257-419164; ARK_ID=JSab96642ad0be8ad4497efc10470da914ab96; _ga=GA1.2.2113072934.1541749177; bdshare_firstime=1552284998707; UserName=Gao__xi; UserInfo=1f19e70697044f69a582ff8566e6704c; UserToken=1f19e70697044f69a582ff8566e6704c; UserNick=Gao__xi; AU=D95; UN=Gao__xi; BT=1552571229457; smidV2=2018072507375427dd73ed5f7d9cfc48b3b033d9176fc20009297de5744a2e0; c_adb=1; firstDie=1; dc_session_id=10_1552741480110.210074; TINGYUN_DATA=%7B%22id%22%3A%22-sf2Cni530g%23HL5wvli0FZI%22%2C%22n%22%3A%22WebAction%2FCI%2FarticleList%252Flist%22%2C%22tid%22%3A%22619a29e5f1d922%22%2C%22q%22%3A0%2C%22a%22%3A26%7D; dc_tos=pogqv2; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1552732069,1552741154,1552741480,1552742210; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1552746494; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=1788*1*PC_VC!5744*1*Gao__xi!6525*1*10_28867322950-1540695012257-419164"
}
html= requests.get(url,header)
soup=BeautifulSoup(html.text,"lxml")
print(type(soup.prettify()))
print(type(soup.code)) #Tag对象
print(type(soup.code.string)) #NaviableString
print(type(soup.article)) #
'''
前面是热身下面进入各个对象的属性
1.Tag对象
1.1 name attrs ['id'] ['class']
1.2 可以 .string 获得NavigableString .strings (就是获取标签里的文字内容)
1.3 可以通过 .<标签名> 获取子标签 并返回Tag
2.NavigableString
3.BeautifulSoup
3.1个人理解就是一个最大的Tag对象
4.Comment(注释)
3.2就是一个特殊的Navigablestring 通过 标签名.string 的方式获取
'''
#Tag对象的
articletag=soup.article
print(articletag.name) #tag对象 name属性
print(articletag.code) #获取子标签 code标签
print(articletag["class"]) #tag对象 class 属性 返回是list
print(articletag.attrs) #tag 对象 所有标签属性 (可以有 class id name)
print(type(articletag.attrs)) #返回为dict 字典
###也可以对其标签设置属性
articletag["class"]='gaoxi' #设置单属性
##打印验证print(articletag)
articletag["class"]=['mytag','James']
##打印验证print(articletag)
###string strings
print("tag对象的.string方法返回内容"+soup.title.string)
print("tag对象的.string方法返回类型")
print(type(soup.title.string))#<class 'bs4.element.NavigableString'>
print(list(soup.title.strings))#获取全部内容
print(type(soup.title.strings))#返回类型 <class 'generator'> 可转成list列表类型
print(len(list(articletag.strings)))
print(list(articletag.strings))
print(list(articletag.stripped_strings)) #去除换行的所有内容
print(len(list(articletag.stripped_strings)))
####NaviableString
####Beautifulsoup 对象
#
print(soup.name)
###Comment