python提取网页表格并保存为csv
0.
1.参考
表格标签
| 表格 | 描述 |
|---|---|
| <table> | 定义表格 |
| <caption> | 定义表格标题。 |
| <th> | 定义表格的表头。 |
| <tr> | 定义表格的行。 |
| <td> | 定义表格单元。 |
| <thead> | 定义表格的页眉。 |
| <tbody> | 定义表格的主体。 |
| <tfoot> | 定义表格的页脚。 |
| <col> | 定义用于表格列的属性。 |
| <colgroup> | 定义表格列的组。 |
表格元素定位

参看网页源代码并没有 thead 和 tbody。。。
<table class="wikitable sortable" style="text-align: center; font-size: 85%; width: auto; table-layout: fixed;">
<caption>List of text editors</caption>
<tr>
<th style="width: 12em">Name</th>
<th>Creator</th>
<th>First public release</th>
<th data-sort-type="number">Latest stable version</th>
<th>Latest Release Date</th>
<th><a href="/wiki/Programming_language" title="Programming language">Programming language</a></th>
<th data-sort-type="currency">Cost (<a href="/wiki/United_States_dollar" title="United States dollar">US$</a>)</th>
<th><a href="/wiki/Software_license" title="Software license">Software license</a></th>
<th><a href="/wiki/Free_and_open-source_software" title="Free and open-source software">Open source</a></th>
<th><a href="/wiki/Command-line_interface" title="Command-line interface">Cli available</a></th>
<th>Minimum installed size</th>
</tr>
<tr>
<th
2.提取表格数据
表格标题可能出现超链接,导致标题被拆分,
也可能不带表格标题。。
<caption>Text editor support for remote file editing over
<a href="/wiki/Lists_of_network_protocols" title="Lists of network protocols">network protocols</a>
</caption>
表格内容换行
<td>
<a href="/wiki/Plan_9_from_Bell_Labs" title="Plan 9 from Bell Labs">Plan 9</a>
and
<a href="/wiki/Inferno_(operating_system)" title="Inferno (operating system)">Inferno</a>
</td>
tag 规律
| table | ||||
| thead tr1 | th | th | th | th |
| tbody tr2 | td/th | td | ||
| tbody tr3 | td/th | |||
| tbody tr3 | td/th | |||
2.1提取所有表格标题列表
filenames = []
for index, table in enumerate(response.xpath('//table')):
caption = table.xpath('string(./caption)').extract_first() #提取caption tag里面的所有text,包括子节点内的和文本子节点,这样也行 caption = ''.join(table.xpath('./caption//text()').extract())
filename = str(index+1)+'_'+caption if caption else str(index+1) #xpath 要用到 table 计数,从[1]开始
filenames.append(re.sub(r'[^\w\s()]','',filename)) #移除特殊符号
In [233]: filenames
Out[233]:
[u'1_List of text editors',
u'2_Text editor support for various operating systems',
u'3_Available languages for the UI',
u'4_Text editor support for common document interfaces',
u'5_Text editor support for basic editing features',
u'6_Text editor support for programming features (see source code editor)',
u'7_Text editor support for other programming features',
'',
u'9_Text editor support for key bindings',
u'10_Text editor support for remote file editing over network protocols',
u'11_Text editor support for some of the most common character encodings',
u'12_Right to left (RTL) bidirectional (bidi) support',
u'13_Support for newline characters in line endings']
2.2每个表格分别写入csv文件
for index, filename in enumerate(filenames):
print filename
with open('%s.csv'%filename,'wb') as fp:
writer = csv.writer(fp)
for tr in response.xpath('//table[%s]/tr'%(index+1)):
writer.writerow([i.xpath('string(.)').extract_first().replace(u'\xa0', u' ').strip().encode('utf-8','replace') for i in tr.xpath('./*')]) #xpath组合,限定 tag 范围,tr.xpath('./th | ./td')
代码处理 .replace(u'\xa0', u' ')
HTML转义字符&npsp;表示non-breaking space,unicode编码为u'\xa0',超出gbk编码范围?
使用 'w' 写csv文件,会出现如下问题,使用'wb' 即可解决问题
【已解决】Python中通过csv的writerow输出的内容有多余的空行 – 在路上
所有表格写入同一excel文件的不同工作表 sheet,需要使用xlwt
python ︰ 创建 excel 工作簿和倾倒 csv 文件作为工作表
python提取网页表格并保存为csv的更多相关文章
- Python读取网页表格数据
学会了从网格爬取数据,就可以告别从网站一页一页复制表格数据的时代了. 说个亲身经历的事: 以前我的本科毕业论文是关于"燃放烟花爆竹和空气质量"之间关系的,就要从环保局官网查资料. ...
- python提取分析表格数据
#/bin/python3.4# -*- coding: utf-8 -*- import xlrd def open_excel(file="file.xls"): try: d ...
- python爬取昵称并保存为csv
代码: import sys import io import re sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') ...
- python爬取信息并保存至csv
import csv import requests from bs4 import BeautifulSoup res=requests.get('http://books.toscrape.com ...
- 使用python 提取网页的特定数据转
http://blog.csdn.net/nwpulei/article/details/7272832
- Python:提取网页中的电子邮箱
import requests, re #regex = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"#这个正则表达式过滤 ...
- Python使用Tabula提取PDF表格数据
今天遇到一个批量读取pdf文件中表格数据的需求,样式大体是以下这样: python读取PDF无非就是三种方式(我所了解的),pdfminer.pdf2htmlEX 和 Tabula.综合考虑后,选择了 ...
- python笔记之提取网页中的超链接
python笔记之提取网页中的超链接 对于提取网页中的超链接,先把网页内容读取出来,然后用beautifulsoup来解析是比较方便的.但是我发现一个问题,如果直接提取a标签的href,就会包含jav ...
- python学习笔记——爬虫中提取网页中的信息
1 数据类型 网页中的数据类型可分为结构化数据.半结构化数据.非结构化数据三种 1.1 结构化数据 常见的是MySQL,表现为二维形式的数据 1.2 半结构化数据 是结构化数据的一种形式,并不符合关系 ...
随机推荐
- VMware Workstation 12 Pro安装CentOs图文教程(超级详细)
本文记录了VMware Workstation 12 Pro安装CentOs的整个过程,具体如下: VMware Workstation 12: CENTOS 6.4 : 创建虚拟机 1.首先安装好V ...
- 两小时入门Docker
引入 Docker是什么? Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,于 2013 年 3 月以 Apache 2.0 授权协议 ...
- vue之——从彩笔的进步之路
因为这个文章开的有点晚,不可能说从头教学vue的使用,所以大概还是记录一下我的学习路线吧: 一开始是想学一个前端框架,最后选择了vue,一开始是看了表严肃的vue课程,b站有,讲的相当好,就算打个小广 ...
- [PA2014]Druzyny
题目描述 体育课上,n个小朋友排成一行(从1到n编号),老师想把他们分成若干组,每一组都包含编号连续的一段小朋友,每个小朋友属于且仅属于一个组.第i个小朋友希望它所在的组的人数不多于d[i],不少于c ...
- beam 的异常处理 Error Handling Elements in Apache Beam Pipelines
Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...
- SpringMVC中使用 MultipartFile 进行文件上传下载及删除
一:引入必要的包 <!--文件上传--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fil ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
- 什么是javabean及其用法(转)
因工作需要 需要学习jsp方面的知识 这篇博客文章写得比较全面 我记录下: 一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个 ...
- HDU2859 Phalanx (动态规划)
Today is army day, but the servicemen are busy with the phalanx for the celebration of the 60th anni ...
- netCore2.0 Api 跨域(Cors)
1.在使用netCore2.0 使用WebApi的过程中涉及到了跨域处理. 在Microsoft.AspNetCore.All包中包含跨域Cors的处理,不必单独添加. 2.打开Startup.cs文 ...