爬虫学习5.1.2数据存储(无数据库)存储为CSV
来源:互联网 发布:淘宝网上有卖药的吗 编辑:程序博客网 时间:2024/05/26 17:49
1.写CSV文件
import csvheaders=['ID','Username','Password','Age','Country']rows=[(1001,"qiye","qiye_pass","24","China"), (1002, "Marry", "Marry_pass", "21", "USA"), (1003, "Jack", "Jack_pass", "20", "USA"),]with open('qiye.csv','w') as f: f_csv= csv.writer(f) f_csv.writerow(headers) f_csv.writerows(rows)
2.字典数据
#row列表里的数据 可以是字典数据import csvheaders=['ID','Username','Password','Age','Country']rows=[{'ID':1001,'Username':"qiye",'Password':"qiye_pass",'Age':"24",'Country':"China"}, {'ID':1002,'Username':"Marry",'Password':"Marry_pass",'Age':"21",'Country':"USA"}, {'ID':1003,'Username':"Jack",'Password':"Jack_pass",'Age':"20",'Country':"USA"},]with open('qiye2.csv','w') as f: f_csv= csv.DictWriter(f,headers) f_csv.writeheader() f_csv.writerows(rows)
3.读取数据(列表读取)
import csvwith open('qiye.csv') as f: f_csv = csv.reader(f) headers = next(f_csv) print headers for row in f_csv: print row
4.读取数据(索引读取)
python的namedtuple详解:http://blog.csdn.net/kongxx/article/details/51553362
命名分组 然后填入数据,提取数据
import csvfrom collections import namedtuplewith open('qiye2.csv') as f: f_csv = csv.reader(f) headings = next(f_csv)#迭代获取对象元素 Row = namedtuple('Row',headings)#定义namedtuple类型 for r in f_csv: row = Row(*r)#获取每行数据创建一个对象 print row.Username,row.Password print row5.读取到一个字典序列中
import csvwith open('qiye.csv') as f: f_csv=csv.DictReader(f) for row in f_csv: print row.get('Username'),row.get('Password')
6.使用lxml解析 http://seputu.com首页标题 章节和链接等数据
#coding:utf-8from lxml import etreeimport requestsimport reimport csvuser_agent = 'Monzilia/4.0 (compatible; MISE 5.5;Windows NT) 'headers = {'User_Agent': user_agent}r = requests.get('http://seputu.com', headers=headers)#使用lxml解析网页html = etree.HTML(r.text)div_mulus = html.xpath('.//*[@class="mulu"]')#先找到所有的div class=mulu标记pattern = re.compile(r'\s*\[(.*)\]\s+(.*)')rows=[]for div_mulu in div_mulus: #找到所有div_h2标记 div_h2 = div_mulu.xpath('./div[@class="mulu-title"]/center/h2/text()') if len(div_h2) > 0: h2_title = div_h2[0].encode('utf-8') a_s = div_mulu.xpath('./div[@class="box"]/ul/li/a') for a in a_s: #找到href属性 href = a.xpath('./@href')[0].encode('utf-8') #找到title属性 box_title = a.xpath('./@title')[0] pattern = re.compile(r'\s*\[(.*)\]\s+(.*)') match = pattern.search(box_title) if match!=None: date = match.group(1).encode('utf-8') real_title=match.group(2).encode('utf-8') #print real_title content = (h2_title,real_title,href,date) print content rows.append(content)headers = ['title','real_title','href','date']with open('qiye.csv','w') as f: f_csv = csv.writer(f,) f_csv.writerow(headers) f_csv.writerows(rows)print 'success!'
阅读全文
0 0
- 爬虫学习5.1.2数据存储(无数据库)存储为CSV
- 数据存储(无数据库版)之二:存储为CSV
- 关于Python爬虫无数据库的存储1之存储为csv格式
- 数据存储(无数据库版)之一:存储为JSON
- python网络爬虫-数据存储之CSV
- 使用csv存储爬虫数据乱码问题解决
- C#将数据存储为CSV文件格式
- python爬虫:使用Mongodb数据库存储数据学习笔记
- [python爬虫] BeautifulSoup爬取+CSV存储贵州农产品数据
- 数据存储之CSV
- python网络爬虫-数据存储中如何改变mysql数据库的字符为unicode
- 使用CSV存储游戏数据
- c#读取csv文件成DataTable,将DataTable数据存储为csv格式文件
- java爬虫抓取数据,存储为excel文件
- 数据存储(3)---数据库存储
- 数据存储详解(三)---->数据库存储
- Android数据存储(SqLite数据库存储)
- 数据存储(2)使用SQL数据库
- 浮动之热门活动页面
- Java file对象 删除目录
- 二叉树的遍历
- TensorFlow模型的保存和持久化
- 枚举属性和不可枚举属性
- 爬虫学习5.1.2数据存储(无数据库)存储为CSV
- Nuxtjs vue-resource element 安装及使用
- Java Properties集合的 基本功能 方法
- 深入理解HTTP协议
- Pow(x, n)--LeetCode
- mybatis逆向工程使用(另附逆向工程项目代码)
- Linux基本操作
- 心斋-------马克奥勒流
- Android 源码分析之——Activity启动流程