优化代码#coding=utf-8 import requests from bs4 import BeautifulSoup url = 'https://coi.hzau.edu.cn/' header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' +\ 'AppleWebKit/537.36 (KHTML, like Gecko)' +\ 'Chrome/113.0.0.0' +\ 'Safari/537.36 Edg/113.0.1774.57'} response = requests.get(url, headers=header) soup = BeautifulSoup(response.text, 'html.parser') articles = soup.select(".list_right .list_right_list li") teacher_list = soup.find_al1("li", class_="name") for teacher in teacher_list: name = teacher.a.text.strip()#获取老师姓名 position = teacher.span.text.strip()#获取老师职位 print(f"姓名:{name}") print(f"职位:{position}") print("-------------")
时间: 2024-04-28 19:26:24 浏览: 108
import reimport requestsfrom bs4 import BeautifulSoupimport t
以下是优化的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://coi.hzau.edu.cn/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
teacher_list = soup.select("ul.job-list li.name")
for teacher in teacher_list:
name = teacher.a.text.strip()
position = teacher.span.text.strip()
print(f"姓名:{name}")
print(f"职位:{position}")
print("-------------")
```
优化的地方包括:
1. 将请求头信息放在一个字典中,使代码更加简洁。
2. 将文章列表的选择器和老师列表的选择器放在一个列表里,提高代码可维护性。
3. 使用 CSS 选择器代替 find_all() 方法,提高代码的效率。
4. 将老师列表的选择器修改为 ".name",可以直接获取所有老师的信息。
阅读全文