本帖最后由 苏紫方璇 于 2025-3-10 13:36 编辑
[Python] 纯文本查看 复制代码 import requests
from bs4 import BeautifulSoup
def extract_latest_news(url):
# 设置请求头模拟浏览器访问‌:ml-citation{ref="1,5" data="citationList"}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
try:
# 发送HTTP请求‌:ml-citation{ref="2,4" data="citationList"}
response = requests.get(url, headers=headers, timeout=10)
response.encoding = response.apparent_encoding # 自动检测编码
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# 查找包含关键字的元素‌:ml-citation{ref="1,3" data="citationList"}
results = []
for element in soup.find_all(['h1', 'h2', 'h3', 'p', 'div']): # 常见文本容器标签
text = element.get_text(strip=True)
if '最新消息' in text:
# 提取关联链接‌:ml-citation{ref="6,7" data="citationList"}
link = element.find_parent('a', href=True)
results.append({
"text": text,
"link": link['href'] if link else url
})
return results
else:
print(f"请求失败,状态码:{response.status_code}")
return []
except Exception as e:
print(f"发生异常:{str(e)}")
return []
# 使用示例
if __name__ == "__main__":
target_url = "https://news.example.com" # 替换为目标网址
news_list = extract_latest_news(target_url)
print(f"找到{len(news_list)}条最新消息:")
for index, news in enumerate(news_list, 1):
print(f"{index}. 内容:{news['text']}")
print(f" 链接:{news['link']}\n") |