python简单爬虫 使用pandas解析表格,不规则表格
url = http://www.hnu.edu.cn/xyxk/xkzy/zylb.htm
部分表格如图:

部分html代码:
<table class="MsoNormalTable" style="width:353.0pt;margin-left:4.65pt;border-collapse:collapse;border:none; mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext" width="471" cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr class="firstRow" style="mso-yfti-irow:0;mso-yfti-firstrow:yes;height:36.75pt">
<td style="width:170.0pt;border:solid windowtext 1.0pt;mso-border-alt: solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:36.75pt" width="227"><p class="MsoNormal" style="text-align:center;margin-top:6.0pt;margin-right:0cm; margin-bottom:6.0pt;margin-left:0cm;mso-para-margin-top:.5gd;mso-para-margin-right: 0cm;mso-para-margin-bottom:.5gd;mso-para-margin-left:0cm; mso-pagination:widow-orphan"><strong><span style="font-size:9.0pt;font-family: 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">学院<span lang="EN-US">
<o:p></o:p></span></span></strong></p></td>
<td style="width:183.0pt;border:solid windowtext 1.0pt; border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt: solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:36.75pt" width="244" nowrap=""><p class="MsoNormal" style="text-align:center;margin-top:6.0pt;margin-right:0cm; margin-bottom:6.0pt;margin-left:0cm;mso-para-margin-top:.5gd;mso-para-margin-right: 0cm;mso-para-margin-bottom:.5gd;mso-para-margin-left:0cm; mso-pagination:widow-orphan"><strong><span style="font-size:9.0pt;font-family: 宋体;mso-bidi-font-family:宋体;mso-font-kerning:0pt">专业名称<span lang="EN-US">
<o:p></o:p></span></span></strong></p></td>
</tr>
<tr style="mso-yfti-irow:1;height:16.5pt">
<td rowspan="4" style="width:170.0pt;border:solid windowtext 1.0pt; border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt; padding:0cm 5.4pt 0cm 5.4pt;height:16.5pt" width="227"><p class="MsoNormal" style="text-align:center;margin-top:6.0pt;margin-right:0cm; margin-bottom:6.0pt;margin-left:0cm;mso-para-margin-top:.5gd;mso-para-margin-right: 0cm;mso-para-margin-bottom:.5gd;mso-para-margin-left:0cm; mso-pagination:widow-orphan"><span style="font-size:9.0pt;font-family:宋体; mso-bidi-font-family:宋体;mso-font-kerning:0pt">土木工程学院<span lang="EN-US">450
<o:p></o:p></span></span></p></td>
<td style="width:183.0pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt; mso-border-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;height:16.5pt" width="244" nowrap=""><p class="MsoNormal" style="text-align:center;margin-top:6.0pt;margin-right:0cm; margin-bottom:6.0pt;margin-left:0cm;mso-para-margin-top:.5gd;mso-para-margin-right: 0cm;mso-para-margin-bottom:.5gd;mso-para-margin-left:0cm; mso-pagination:widow-orphan"><span style="font-size:9.0pt;font-family:宋体; mso-bidi-font-family:宋体;mso-font-kerning:0pt">土木工程<span lang="EN-US">
<o:p></o:p></span></span></p></td>
</tr>
......
</tbody>
</table>
用pandas解析表格,代码如下:
import pandas as pd
url = 'http://www.hnu.edu.cn/xyxk/xkzy/zylb.htm' table = pd.read_html(url)
pd.set_option('display.max_rows', None) # 显示全部的行
with open("湖南大学学院与专业.txt", "wt", encoding='utf8') as out_file: # 保存为txt文件
for i in table:
out_file.write(str(i)+'\n')
运行结果如下(部分):

非常简洁高效!
python简单爬虫 使用pandas解析表格,不规则表格的更多相关文章
- python简单爬虫 用lxml解析页面中的表格
目标:爬取湖南大学2018年在各省的录取分数线,存储在txt文件中 部分表格如图: 部分html代码: <table cellspacing="0" cellpadding= ...
- Python简单爬虫入门三
我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...
- Python简单爬虫入门二
接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...
- GJM : Python简单爬虫入门(二) [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- Python 简单爬虫案例
Python 简单爬虫案例 import requests url = "https://www.sogou.com/web" # 封装参数 wd = input('enter a ...
- python简单爬虫一
简单的说,爬虫的意思就是根据url访问请求,然后对返回的数据进行提取,获取对自己有用的信息.然后我们可以将这些有用的信息保存到数据库或者保存到文件中.如果我们手工一个一个访问提取非常慢,所以我们需要编 ...
- Python简单爬虫
爬虫简介 自动抓取互联网信息的程序 从一个词条的URL访问到所有相关词条的URL,并提取出有价值的数据 价值:互联网的数据为我所用 简单爬虫架构 实现爬虫,需要从以下几个方面考虑 爬虫调度端:启动爬虫 ...
- python 简单爬虫(beatifulsoup)
---恢复内容开始--- python爬虫学习从0开始 第一次学习了python语法,迫不及待的来开始python的项目.首先接触了爬虫,是一个简单爬虫.个人感觉python非常简洁,相比起java或 ...
- Python简单爬虫记录
为了避免自己忘了Python的爬虫相关知识和流程,下面简单的记录一下爬虫的基本要求和编程问题!! 简单了解了一下,爬虫的方法很多,我简单的使用了已经做好的库requests来获取网页信息和Beauti ...
随机推荐
- windows下安装git和vundle
git在windows下的版本是: git-for-windows, 或者说是: msysgit: ms-sys-git 直接在 https://gitforwindows.org/上下载 git对w ...
- Java学习笔记_ 数据库隔离级别和事务传播行为
转自: https://blog.csdn.net/qq_38545713/article/details/79779265 一:数据库的事物隔离级别与传播行为总结: 很多人 ...
- ModelBiner不验证某个属性
问题 使用MVC的同学十有八九都会遇到这个错误:从客户端(Content="<script>...")中检测到有潜在危险的Request.Form 值. 这个错误是在请 ...
- OpenGL.Tutorial15_Lightmaps
ZC:撤销 & 重做 — Blender Manual.html(https://docs.blender.org/manual/zh-hans/dev/interface/undo_redo ...
- 实验八 <FBG> 基于原型的团队项目需求调研与分析
<FBG>团队项目原型设计:http://www.cnblogs.com/ymm3/p/9012534.html GitHub的链接地址:https://github.com/FBGfbg ...
- jenkins构建成功,但war包没有发布到tomcat下
如题,jenkins构建成功,在jenkins的workspace中有生成的war包,但没有发布到tomcat的webapps目录. 构建日志 找了很多原因发现应该还是项目相对路径不对导致的,我的wa ...
- Phonics 自然拼读法 g, o, u, l, f, b Teacher:Lamb
课上内容(Lesson) Part I Warm-up & Video Food Fashion Fun 19 sets http://www.unsv.com/material/ ...
- SVN-Failed to run the WC DB work queue associated with
解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db 3.将sqlite3.exe放到.svn的同级目录 4.启动cmd执行sqlite3 ...
- 安装和启动json-server
安装json-server JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源 npm i -g json-server ...
- jquery tab切换
首先引入jquery.js <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...