cookie做网站登录,京东app官网下载,网页版微信登不上去怎么回事,书写网站建设策划书近一个月左右的时间学习爬虫#xff0c;在用所积累的知识爬取了《中国大学排名》这个网站#xff0c;爬取的内容虽然只是可见的文本#xff0c;但对于初学者来说是一个很好的练习。在爬取的过程中#xff0c;通过请求数据、解析内容、提取文本、存储数据等几个重要的内容入… 近一个月左右的时间学习爬虫在用所积累的知识爬取了《中国大学排名》这个网站爬取的内容虽然只是可见的文本但对于初学者来说是一个很好的练习。在爬取的过程中通过请求数据、解析内容、提取文本、存储数据等几个重要的内容入手不过在存储数据后的数据排版方面并不是很完善优化希望阅读本文章的学者大大给些存储后的数据排版方面的指点中文对齐的问题 文章目录 前言一、从网络上获取大学排名网页内容— getHTMLText()二、提取网页内容中信息到合适的数据结构— fillUnivList()三、将数据保存至电脑文件夹中— Store_as_file()四、主函数总结 前言
本次案例主要涉及bs4库中的BeautifulSoup内容、requests的使用和存储数据等知识。 提示以下是本篇文章正文内容下面案例可供参考
一、从网络上获取大学排名网页内容— getHTMLText()
爬取的网址https://www.shanghairanking.cn/rankings/bcur/202411判断是否可以爬取 在该网站的根目录下查看robots.txt文件是否可以爬取内容这里显示没有搜索到该内容
3.利用request库爬取
def getHTMLText(url):try:r requests.get(url, timeout30)r.raise_for_status() # 判断请求是否成功如果不是200产生异常requests.HTTPErrorr.encoding r.apparent_encoding # http header中猜测的响应内容编码方式 设置为 内容中分析出的响应内容编码方式(备选编码方式)return r.textexcept:return 请求失败二、提取网页内容中信息到合适的数据结构— fillUnivList()
分析网页 我们要爬取的是”排名“”学校名称“”省市“”类型“”总分“”办学层次“等信息如图
先是分析整体信息需要爬取的文本信息都存放在.html网页中的tbody/tbody中的tr标签下. ”学校名称”在div classunivname data-v-90b0d2ac标签下a标签中。 特征a的父亲div标签的属性都是classlink-container和stylewidth:200px 而”省市“”类型“”总分“”办学层次“等都是直接在tr标签的子代中所以可以直接获取相关数据存放至列表中
解析数据 获取主要爬取的数据存放至列表中并返回
def fillUnivList(ulist, html):soup BeautifulSoup(html, html.parser) # 设置BeautifulSoup解析器为html.parsersoup.prettify() # 整理解析的网页# 创建列表tds_name []name_types []tds_location []tds_type []tds_total []tds_level []try:# 遍历tbody的下行遍历for tr in soup.tbody.children:# 检测tr标签的类型的类型如果tr标签的类型不是bs4库定义的tag类型将过滤掉if isinstance(tr, bs4.element.Tag): # 检查变量tr是否为BeautifulSoup库中Tag类的实例的一个条件判断语句# tdsstr(list(tr(td)[2])[0]).strip()# 学校名称td_name tr(td)[1]td_div_names td_name.find_all(div, attrs{style: width:200px, class: link-container})for div_tag in td_div_names:# 另一种写法# name_part div_tag.find(a).get_text(stripTrue).split(\n, 1)[0]a str(div_tag.find_all(a)[0].string).strip().split(\n)[0]tds_name.append(a)# 学校类型td_name_type tr(td)[1] \.find_all(div, attrs{class: univname})[0] \.find_all(p, attrs{class: tags})[0].get_text(stripTrue)# 位置td_location tr(td)[2].get_text(stripTrue)# 类型td_type tr(td)[3].get_text(stripTrue)# 总分td_total tr(td)[4].get_text(stripTrue)# 办学层次td_level tr(td)[5].get_text(stripTrue)# 将各个数据添加至列表name_types.append(td_name_type)tds_location.append(td_location)tds_type.append(td_type)tds_total.append(td_total)tds_level.append(td_level)# break# 中文名字列表name_cns tds_name[::2]# 英文名字列表name_ens tds_name[1::2]i1# 遍历列表大学信息存放至空列表university中使用zip打包zip打包后的数据是元组for name_cn, name_en, name_type, location, type, total, level in \zip(name_cns, name_ens, name_types, tds_location, tds_type, tds_total, tds_level):university_data {序号:i,学校名称: name_cn name_en name_type,省市: location,类型: type,总分: total,办学层次: level}i1ulist.append(university_data)return ulistexcept:return 爬取失败三、将数据保存至电脑文件夹中— Store_as_file()
这里直接给出代码块因为完全没有真的优化处理好爬取后的数据还是很杂乱
def Store_as_file(path,datas):# 打开文件准备写入with open(path, w, encodingutf-8) as file:# 写入表头方便阅读file.write({:^10}\t{:110}\t{:10}\t{:10}\t{:10}\t{:10}\n.format(序号,学校名称,省市,类型,总分,办学层次))t\t*10# file.write(f序号\t学校名称\t\t省市\t\t类型\t\t总分\t\t办学层次\n)# 遍历列表将每个字典的内容写入文件for university in datas:# 使用制表符分隔各个字段保证对齐line {序号:^10}\t{学校名称:110}\t{省市:10}\t{类型:10}\t{总分:10}\t{办学层次:10}\n.format(**university)file.write(line)print(f数据已成功保存至{path})四、主函数
代码块主函数的书写
def main():university []num int(input(请输入大学排名的年份))urlfhttps://www.shanghairanking.cn/rankings/bcur/{num}11htmlgetHTMLText(url)datasfillUnivList(university,html)pathinput(请输入存放内容的位置)Store_as_file(path,datas)最终效果当然我是确实不知道怎么更改还望读者帮忙提供点意见 总结
总代码块导入requests库和bs4库和bs4库中的BeautifulSoup
import requests
from bs4 import BeautifulSoup
import bs4def getHTMLText(url):try:r requests.get(url, timeout30)r.raise_for_status() # 判断请求是否成功如果不是200产生异常requests.HTTPErrorr.encoding r.apparent_encoding # http header中猜测的响应内容编码方式 设置为 内容中分析出的响应内容编码方式(备选编码方式)return r.textexcept:return 请求失败def fillUnivList(ulist, html):soup BeautifulSoup(html, html.parser) # 设置BeautifulSoup解析器为html.parsersoup.prettify() # 整理解析的网页# 创建列表tds_name []name_types []tds_location []tds_type []tds_total []tds_level []try:# 遍历tbody的下行遍历for tr in soup.tbody.children:# 检测tr标签的类型的类型如果tr标签的类型不是bs4库定义的tag类型将过滤掉if isinstance(tr, bs4.element.Tag): # 检查变量tr是否为BeautifulSoup库中Tag类的实例的一个条件判断语句# tdsstr(list(tr(td)[2])[0]).strip()# 学校名称td_name tr(td)[1]td_div_names td_name.find_all(div, attrs{style: width:200px, class: link-container})for div_tag in td_div_names:# 另一种写法# name_part div_tag.find(a).get_text(stripTrue).split(\n, 1)[0]a str(div_tag.find_all(a)[0].string).strip().split(\n)[0]tds_name.append(a)# 学校类型td_name_type tr(td)[1] \.find_all(div, attrs{class: univname})[0] \.find_all(p, attrs{class: tags})[0].get_text(stripTrue)# 位置td_location tr(td)[2].get_text(stripTrue)# 类型td_type tr(td)[3].get_text(stripTrue)# 总分td_total tr(td)[4].get_text(stripTrue)# 办学层次td_level tr(td)[5].get_text(stripTrue)# 将各个数据添加至列表name_types.append(td_name_type)tds_location.append(td_location)tds_type.append(td_type)tds_total.append(td_total)tds_level.append(td_level)# break# 中文名字列表name_cns tds_name[::2]# 英文名字列表name_ens tds_name[1::2]i1# 遍历列表大学信息存放至空列表university中使用zip打包zip打包后的数据是元组for name_cn, name_en, name_type, location, type, total, level in \zip(name_cns, name_ens, name_types, tds_location, tds_type, tds_total, tds_level):university_data {序号:i,学校名称: name_cn name_en name_type,省市: location,类型: type,总分: total,办学层次: level}i1ulist.append(university_data)return ulistexcept:return 爬取失败def Store_as_file(path,datas):# 打开文件准备写入with open(path, w, encodingutf-8) as file:# 写入表头方便阅读file.write({:^10}\t{:110}\t{:10}\t{:10}\t{:10}\t{:10}\n.format(序号,学校名称,省市,类型,总分,办学层次))t\t*10# file.write(f序号\t学校名称\t\t省市\t\t类型\t\t总分\t\t办学层次\n)# 遍历列表将每个字典的内容写入文件for university in datas:# 使用制表符分隔各个字段保证对齐line {序号:^10}\t{学校名称:110}\t{省市:10}\t{类型:10}\t{总分:10}\t{办学层次:10}\n.format(**university)file.write(line)print(f数据已成功保存至{path})def main():university []num int(input(请输入大学排名的年份))urlfhttps://www.shanghairanking.cn/rankings/bcur/{num}11htmlgetHTMLText(url)datasfillUnivList(university,html)pathinput(请输入存放内容的位置)Store_as_file(path,datas)if __name__ __main__:main()最后还是想哆嗦一下希望读者大大和爬虫感兴趣的多找我讨论讨论给出点建议和学习上的交流
文章转载自: http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.hnrls.cn.gov.cn.hnrls.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn