import requests
from bs4 import BeautifulSoup
发送 GET 请求获取网页内容
url = “https://www.astm.org/f0963-23.html” #先从ASTM网站确认下需查询的标准的网址
print(f’ASTM标准的查询网址:{url}') #输出查询网址
response = requests.get(url)
使用 BeautifulSoup 解析网页内容
soup = BeautifulSoup(response.text, ‘html.parser’)
#print(soup)
获取标准编号
titles = soup.find_all(‘b’, class_=‘sku’)
for title in titles:
title1 = title.text.strip()
获取标准名称
names = soup.find_all(‘b’, class_=‘name’)
for name in names:
name1 = name.text
print(title1, name1)
查找所有 class 为 dropdown-item pl-3 的 span 标签
print(‘ASTM标准历史版本的查询网址:’)
span_tags = soup.find_all(‘span’, class_=‘dropdown-item pl-3’)
for span_tag in span_tags:
# 在每个 span 标签中查找紧跟其后的 a 标签的 href 属性值
a_tag = span_tag.find_next(‘a’)
if a_tag and ‘href’ in a_tag.attrs:
print(a_tag[‘href’])
查找所有 class 为 subsec1 cdone2 的 p 标签并提取文本内容
print(‘ASTM标准的主要内容:’)
subsec1_cdone2_elements = soup.find_all(‘p’, class_=‘subsec1 cdone2’)
for element in subsec1_cdone2_elements:
print(element.text.strip())