You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用bs4做web scraping时无法正确获取目标星期日期该如何解决

你现有代码的问题是find_all("strong")返回的是BeautifulSoup的Tag对象列表,直接打印会输出完整的标签结构,只需要提取每个标签的文本内容即可。

调整后的代码如下:

import requests
from bs4 import BeautifulSoup
import pandas as pd

page = requests.get('https://weather.gc.ca/city/pages/bc-74_metric_e.html')
html = BeautifulSoup(page.content, 'html.parser')

forecast = html.find(class_="visible-xs mrgn-tp-md")
# 遍历所有strong标签提取文本并打印
for day_tag in forecast.find_all("strong"):
    print(day_tag.text.strip())

运行以上代码即可得到你需要的输出结果。原理是通过.text属性获取标签内部的文本内容,.strip()用于清除文本前后可能存在的多余空白符。


内容的提问来源于stack exchange,提问作者tomatosalad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 06:36:05