使用requests和BeautifulSoup4库爬取并解析OJ网站数据
首先,我们需要安装`requests`和`beautifulsoup4`库,可以使用以下命令安装:
```bash
pip install requests beautifulsoup4
```
接下来,我们将编写一个Python脚本来完成这个任务。以下是解析步骤:
1. 导入所需的库。
2. 使用`requests`库获取网页内容。
3. 使用`BeautifulSoup4`库解析HTML内容。
4. 根据任务要求,输出第一页数据和筛选后的学号提交情况。
以下是完成这个任务的Python代码:
```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())
```
运行这段代码,它将首先输出第一页的数据,然后提示你输入学号,最后输出筛选后的学号提交情况。
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!