爬取齐齐哈尔大学2022年云南录取分数线——二本及预科

发布时间 2023-04-20 18:03:01作者: 峿
import requests
from lxml import etree
head = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36 Edg/110.0.1587.63',
'Connection': 'close'} # 当前正在使用的tcp链接在当天请求处理完毕后会被断掉
url = 'http://zs.qqhru.edu.cn/info/1145/3652.htm'
response = requests.get(url=url,headers=head,)
print(response.encoding)
data = response.text.encode('ISO-8859-1').decode('utf-8') # Python 终端的编码方式是 UTF-8 ,而 HTML 编码方式并不是 UTF-8 (可通过 response.encoding 查询页面编码格式),所以在匹配标题时中文产生了乱码
html = etree.HTML(data)
category = html.xpath('//table/tbody/tr/td[1]/p/span/text()') # 类别
specialized = html.xpath('//table/tbody/tr/td[2]/p/span/text()') # 专业
max_score = html.xpath('//table/tbody/tr/td[3]/p/span/text()') # 最高分
min_score = html.xpath('//table/tbody/tr/td[4]/p/span/text()') # 最低分
average_score = html.xpath('//table/tbody/tr/td[5]/p/span/text()') # 平均分
print('科类',' 专业名称','___','最高分','___','最低分','___','平均分')
for i in range(len(category)):
print(category[i],specialized[i],min_score[i],min_score[i],average_score[i])