记录成长
代码异常简陋,大佬勿笑。
import requests#引用函数库
from lxml import etree#引用函数库
import xlwt#引用函数库
url='https://www.eol.cn/e_html/gk/dxpm/index.shtml'#获取网站链接
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56'}#进行UA伪装
r=requests.get(url=url,headers=headers)#发送请求
r1=etree.HTML(r.content,parser = etree.HTMLParser(encoding='utf8'))#获取响应数据并解析
r2=r1.xpath('//tr[@style="color: #333;"]/td[1]/text()')#利用xpath匹配文本
r3=r1.xpath('//tr[@style="color: #333;"]/td[2]/text()')#利用xpath匹配文本
r4=r1.xpath('//tr[@style="color: #333;"]/td[3]/text()')#利用xpath匹配文本
ranking_list=r2[0:567];name_list=r3[0:567];score_list=r4[0:567]#用列表索引需要的文本
book=xlwt.Workbook()#创建一个excel
sheet1=book.add_sheet('first');i=0;i1=0;i2=0#创建一个名字为first的sheet1
for j in ranking_list:#遍历列表
sheet1.write(i,1,j)#在sheet1中第i行第一列写入j值
i+=1#增强字符叠加
for q in name_list:#遍历列表
sheet1.write(i1,2,q)#在sheet1中第i1行第二列写入q值
i1+=1#增强字符叠加
for k in score_list:#遍历列表
sheet1.write(i2,3,k)#在sheet1中第i2行第三列写入k值
i2+=1#增强字符叠加
book.save('大学排名.xlsx')#创建保存文件夹