Python采集网页数据保存到excel
2013-05-05 17:35
447 查看
urllib读取网页,然后用Py-excel写excel。
import urllib from xlwt import Workbook import datetime def FetchData(): book = Workbook(encoding='gbk') #如果采集数据有中文,需要添加这个 sheet1 = book.add_sheet('Sheet 2') #表格缓存 i = 0 theday = datetime.date(2009,12,31) while i < 100: #这边的场景就是采集100个网页,每个网址都包含日期 i += 1 theday = theday + datetime.timedelta(days = 1) print theday theday_str = str(theday) sheet1.write(i,0,theday_str) #写表格 check_url = r'http://www.xxx.com/index?date=' + theday_str #网页地址 try: checkfile = urllib.urlopen(check_url) #网页保存为文本文件 except Exception,e: print e return type = sys.getfilesystemencoding() for line in checkfile: line = line.decode("UTF-8").encode(type) #网页编码为UTF-8 date_west = getdata('date_west', line) #获取特定数据 if date_west != False: sheet1.write(i,1,date_west) book.save('simple.xls') #保存excel文件 print 'finish!' 'if keywords in the line, get data from > to </' def getdata(keywords, line): data = '' if keywords in line: start = line.find('>',) end = line.find('</', start) data = line[start+1:end] return data return False
相关文章推荐
- 网页数据采集:[3]python如何利用抓包数据
- python[番外篇]:python数据如何保存到excel中
- API例子:用Python驱动Firefox采集网页数据
- Django+python实现网页数据的excel导出
- API例子:用Python驱动Firefox采集网页数据
- Python爬虫实战(4):豆瓣小组话题数据采集―动态网页
- API例子:用Python驱动Firefox采集网页数据
- Selenium学习四——利用Python爬取网页多个页面的表格数据并存到已有的excel中
- [Python]网络数据采集概述(3)—穿越网页表单、登录窗口进行采集
- Selenium学习三——利用Python爬取网页表格数据并存到excel
- Python网络数据采集9:穿越网页表单与登录窗口进行采集
- Python网络数据采集(1)——获取网页源码
- python 将数据保存为excel的xls格式(实例讲解)
- Python数据采集之网页解析器
- 数据保存!!!Python 爬取网页数据后,三种保存格式---保存为txt文件、CSV文件和mysql数据库
- python python 入门学习之网页数据爬虫cnbeta文章保存
- Python3 爬虫拉勾网抓取数据保存在Excel中
- API例子:用Python驱动Firefox采集网页数据
- python 读取txt中每行数据,并且保存到excel中的实例
- python网络数据采集-穿越网页表单进行采集