import requests
from bs4 import BeautifulSoup
headers = {
"User-Agent": "Mozilla/5.0(Windows NT 10.0;WOW64;rv:50.0) AppleWebKit/537.36 (KHTML, like Gecko) Gecko/20100101 Firefox/50.0 Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3",
'Connection': 'keep-alive'}
r=requests.get('http://www.baidu.com',headers=headers)
soup=BeautifulSoup(r.text,"html.parser")
print(soup.a.string)
print(soup.title.text)
print(soup.a)
print(soup.a.name)
print(soup.a.attrs)
print(soup.a['class'])
print(soup.a.get('class'))
(3)直接子节点:.contents .children属性
(4)所有子孙节点:.descendants属性
(5)获取文本:.string属性
(6)父节点:.parent(父亲里边的所有元素) .parents(迭代器)
(7)兄弟节点:.next_sibling .previous_sibling 加s同上
(8)下一个与上一个要解析的元素:.next_elements .previous_elements