源地址

soups  = BeautifulSoup(html)
soup = BeautifulSoup(open('index.html'))

 

print soup.prettify()

 

Tag通俗点讲就是 HTML 中的一个个标签
print (soup.title)
print (soup.head)
print (soup.a)
print (soup.p)
查找的是在所有内容中的第一个符合要求的标签
对于 Tag,它有两个重要的属性,是 name 和 attrs,下面我们分别来感受一下

name
 soup.name
print soup.head.name
#[document]
#head

attrs

print soup.p.attrs
如果我们想要单独获取某个属性,可以这样,例如我们获取它的 class 叫什么
print soup.p['class']
#['title']

还可以这样,利用get方法,传入属性的名称,二者是等价的

 

相关文章:

  • 2022-01-15
  • 2021-12-03
  • 2021-11-06
  • 2022-02-23
  • 2022-02-25
  • 2021-08-30
猜你喜欢
  • 2021-12-22
  • 2022-01-05
  • 2022-01-28
  • 2021-12-17
  • 2022-12-23
  • 2021-10-07
  • 2021-05-19
相关资源
相似解决方案