关于爬虫
  • 板块学术版
  • 楼主tder
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/3/15 21:15
  • 上次更新2024/3/15 21:32:43
查看原帖
关于爬虫
714254
tder楼主2024/3/15 21:15

我想爬比赛的报名人数,文心一言生成:

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?或者有别的写法吗?

2024/3/15 21:15
加载中...