请用python设计一段代码实现以下任务请搜索网站httpswwweeagdeducnlzksyxzycxyxzycxjsp中的内容:已知院校代码为school_index=1000110003请从网站页面中搜索出对应的院校代码并返回对应的院校代码 院校名称 批次 计划类别 招生科类 专业数 计划数 招生范围网站页面由jsp表示如下:院校代码 院校名称 批次 计划类别 招生科类 专业数 计划数 招
以下是示例代码:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 Edg/114.0.1823.51'
}
url = 'https://www.eeagd.edu.cn/lzks/yxzycx/yxzycx.jsp'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
school_index = [10001, 10003]
for tr in soup.find_all('tr'):
tds = tr.find_all('td')
if len(tds) == 8:
code = int(tds[0].text.strip())
if code in school_index:
print(tds[0].text.strip(), tds[1].text.strip(), tds[2].text.strip(), tds[3].text.strip(), tds[4].text.strip(), tds[5].text.strip(), tds[6].text.strip(), tds[7].text.strip())
代码首先设置了User Agent,然后发送请求获取网页内容,并使用BeautifulSoup解析网页。接着,定义了需要搜索的院校代码,遍历页面中所有的表格行,并判断是否有8列,如果有则说明是院校信息,提取出第一列的代码并判断是否在需要搜索的列表中,如果是则打印出对应的信息
原文地址: https://www.cveoy.top/t/topic/hnM7 著作权归作者所有。请勿转载和采集!