>>> from bs4 import BeautifulSoup   #导入

>>> soup = BeautifulSoup(url.content,"lxml")

>>> print(soup.prettify) #格式化代价

>>> print(soup.title.string)
郑州轻工业学院OJ     #打印title

>>> print(soup.title)
<title>郑州轻工业学院OJ</title>

标签选择器:

>>> print(soup.a)
<a class="navbar-brand" href="./" style="font-weight:bold;">郑州轻工业学院OJ</a>

>> print(soup.title.name)
title

以zzuli的zcm网为抓取对象:

>>> print(soup.div.attrs['class'])
['container']
>>> print(soup.nav.attrs['class'])
['navbar', 'navbar-default']
>>> print(soup.nav.attrs['role'])
navigation

BeautifulSoup的基本操作-LMLPHP

(可使用此方法获取标签后对应的内容)

其余的写法:

>>> print(soup.nav['role'])
navigation

05-06 08:04