aiaiask.com©

开花即结果 提问即答案

使用requests和BeautifulSoup4库爬取并解析OJ网站数据
原始问题:

使用requests和BeautifulSoup4库爬取并解析OJ网站数据

首先,我们需要安装requestsbeautifulsoup4库,可以使用以下命令安装:

pip install requests beautifulsoup4

接下来,我们将编写一个Python脚本来完成这个任务。以下是解析步骤:

  1. 导入所需的库。
  2. 使用requests库获取网页内容。
  3. 使用BeautifulSoup4库解析HTML内容。
  4. 根据任务要求,输出第一页数据和筛选后的学号提交情况。

以下是完成这个任务的Python代码:

import requests
from bs4 import BeautifulSoup

# 爬取网址第一页数据
url = "http://172.16.26.123/status.php"
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup4解析HTML内容
soup = BeautifulSoup(html_content, "html.parser")
table = soup.find("table")

# 以html表格方式输出相关内容
print(table.prettify())

# 输入学号,筛选之后以html表格方式输出该学号编程的提交情况
student_id = input("请输入学号:")
filtered_rows = []
for row in table.find_all("tr"):
    cells = row.find_all("td")
    if len(cells) > 0 and cells[0].get_text() == student_id:
        filtered_rows.append(row)

# 输出筛选后的学号提交情况
print("筛选后的学号提交情况:")
for row in filtered_rows:
    print(row.prettify())

运行这段代码,它将首先输出第一页的数据,然后提示你输入学号,最后输出筛选后的学号提交情况。

使用requests和BeautifulSoup4库爬取并解析OJ网站数据
https://www.aiaiask.com/ask/87462.html
AI / 模型DeepSeek
时间 / DATEMay 28, 2024, 9:44 PM
语言zh-CN
IP / 区域广东 广州