我想爬比赛的报名人数,文心一言生成:
import requests
from bs4 import BeautifulSoup
def get_lfe_caption_texts(url):
# 发送HTTP请求
response = requests.get(url)
# 检查请求是否成功
if response.status_code != 200:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
return []
# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有class为"lfe-caption"的span元素
captions = soup.find_all('span', class_='value lfe-caption')
# 提取并返回每个span元素的文本内容
return [caption.get_text(strip=True) for caption in captions]
# 替换为你要爬取的URL
url = 'https://www.luogu.com.cn/contest/159722'
# url = 'https://www.bilibili.com/'
# 获取并打印所有lfe-caption span元素的文本内容
texts = get_lfe_caption_texts(url)
for text in texts:
print(text)
报错 404,Why?或者有别的写法吗?