import requests from bs4 import BeautifulSoup '''Beautiful Soup 学习说明历程 说明在下''' # 拿到 = requests.get('https://www.autohome.com.cn/all/') # # 拿到.encoding='gbk' # # soup = BeautifulSoup(拿到.text,'html.parser') # html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ '''👆 一段HTML代码将作为例子被多次用到. 这是 爱丽丝梦游仙境的 的一段内容 (以后内容中简称为 爱丽丝 的文档): ''' soup = BeautifulSoup(html_doc,'html.parser') #读取入soup # print(soup.prettify()) ##将souo美化后打印下来 # print(soup.title) ##寻找到第一个 title 标签 # print(soup.title.name) ##找到第一个title标签的名字 # print(soup.title.string) ##找到第一个title标签 并找到其字符串 # print(soup.title.parent.name) ##第一个title标签的父标签名字 # print(soup.p) ##寻找到第一个 p 标签 # print(soup.p['class']) ##寻找到第一个 p 标签的class匹配的字符串 # print(soup.a) ####寻找到第一个 a标签 # print(soup.find_all('a')) ##寻找到所有的 a标签 # print(soup.find(id='link3')) ##寻找到ID=link3 的 a标签 for i in soup.find_all('a'): print(i.get('href'))