step1: 安装
pip install bs4 -i +镜像
step2:使用
1. 获取页面源代码
2. BeautifulSoup解析(成树结构),将页面源代码交给BeautifulSoup解析,将解析器设置为html.parser
3. 使用find/find_all函数查找标签
find(标签,属性=值)
find_all(标签,属性=值)
step3:代码实现
import requests
from bs4 import BeautifulSoup
def spier():
url = r"http://www.xinfadi.com.cn/marketanalysis/0/list/1.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
tables = soup.find_all("table", attrs={"class": "hq_table"})
for table in tables:
trs = table.find_all("tr")
for tr in trs:
print(tr)
response.close()
return
if __name__ == '__main__':
spier()