原始问题:
使用requests和BeautifulSoup4库爬取并解析网页内容
首先,我们需要安装requests和beautifulsoup4库,可以使用以下命令安装:
pip install requests beautifulsoup4接下来,我们将编写一个Python脚本来完成这个任务。以下是解析步骤:
- 导入所需的库。
- 使用
requests库获取网页内容。 - 使用
BeautifulSoup4库解析HTML内容。 - 根据任务要求,提取相关信息并以HTML表格形式输出。
以下是完成这个任务的Python代码:
import requests
from bs4 import BeautifulSoup
# 爬取网址第一页数据,以html表格方式输出相关内容
def get_first_page():
url = "http://172.16.26.123/status.php"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')
print(table)
# 输入用户名,以html表格方式输出该用户编程的提交情况
def get_user_submissions(username):
url = f"http://172.16.26.123/status.php?user={username}"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')
print(table)
if __name__ == "__main__":
get_first_page()
username = input("请输入用户名:")
get_user_submissions(username)运行这个脚本,它将首先输出第一页的数据,然后提示你输入用户名,最后输出该用户的提交情况。
Prev:人工湿地对新型污染物的去除作用研究论文