Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57454
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 199
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-07 22:11
文章分类

全部博文(13)

文章存档

2014年(13)

我的朋友

分类: Python/Ruby

2014-05-11 22:13:14

1.功能
在一个每日播报猪价的网页上,抓取近几天的数据,保存在本地文件,然后对文件进行去重复行和空行
2.代码

点击(此处)折叠或打开

  1. # coding: utf8
  2. import urllib.request
  3. import re
  4. import sys
  5. import time


  6. #时间处理
  7. ti=time.asctime()
  8. print (ti)
  9. t=ti[4:7]
  10. #ti_day=ti[8:10]
  11. if t=='Jan':t='1'
  12. elif t=='Feb':t='2'
  13. elif t=='Mar':t='3'
  14. elif t=='Apr':t='4'
  15. elif t=='May':t='5'
  16. elif t=='Jun':t='6'
  17. elif t=='Jul':t='7'
  18. elif t=='Aug':t='8'
  19. elif t=='Sep':t='9'
  20. elif t=='Oct':t='10'
  21. elif t=='Nov':t='11'
  22. elif t=='Dec':t='12'
  23. #print (ti_day)

  24. #在此网页中抓取今天湖南价格的超链接
  25. for f in range(1,4):
  26.     url = '%s' %f
  27.     #print (url)
  28.     url_time= '"h.{53} title="2014年%s月.{1,2}日湖南.{0,2}生猪价格' %(t)
  29.     find_re0 = re.compile(url_time,re.DOTALL)
  30.     #print (url_time)
  31. #用GBK解码
  32.     html = urllib.request.urlopen(url).read().decode('GBK')
  33. # 找到资源信息
  34.     for x in find_re0.findall(html):
  35.     #去掉连接串中多余的部分得到今日的url
  36.         aaa=re.compile(r'h.{52}')
  37.         f=open(r'zhujia.txt','a')
  38.         for yy in aaa.findall(x):
  39.             url=yy
  40.             print (url)

  41. # 匹配规则
  42.             url_time = '湖南省.{3} .{2,3} 生猪价格.{4} %s月.{1,2}日 .{9}' %(t)
  43.             find_re = re.compile(url_time, re.DOTALL)
  44.             print (find_re)
  45.            
  46.                 
  47.             
  48.                 

  49. #打开文件
  50.            
  51. # 下载数据
  52.             html = urllib.request.urlopen(url).read().decode('gb2312')
  53. # 找到资源信息
  54.             if find_re.findall(html)==[]:
  55.                 url_time = '湖南省 .{2,3} 生猪价格.{4} %s月.{1,2}日 .{9}' %(t)
  56.                 find_re = re.compile(url_time, re.DOTALL)
  57.                 print (find_re)
  58.                 for x in find_re.findall(html):
  59.     #写入文件
  60.                     f.writelines(x+'\r\n')
  61.                     print (f.writelines)
  62.             else:
  63.                 for x in find_re.findall(html):
  64.     #写入文件
  65.                     f.writelines(x+'\r\n')
  66.                     print (f.writelines)
  67.                 
  68.                 
  69. #睡眠2秒
  70.                 time.sleep(2)

  71. #因为后面几页匹配要循环遍历几个网页,所以去掉变化的部分用变量替代
  72.             url=url[:-5]

  73.             for y in range(2,5):
  74.                 url_1 = '%s_%s.html' %(url,y)
  75.                 print (url_1)
  76.                 
  77.                 
  78.                 url_time = '湖南省.{3} .{2,3} 生猪价格.{4} %s月.{1,2}日 .{9}'%(t)
  79.                 find_re = re.compile(url_time, re.DOTALL)
  80.                
  81.                    
  82. # 下载数据
  83.                 htm = urllib.request.urlopen(url_1).read().decode('gb2312')
  84. # 找到资源信息
  85.                 time.sleep(2)
  86.                 if find_re.findall(html)==[]:
  87.                     url_time = '湖南省 .{2,3} 生猪价格.{4} %s月.{1,2}日 .{9}' %(t)
  88.                     find_re = re.compile(url_time, re.DOTALL)
  89.                     print (find_re)
  90.                     for x in find_re.findall(html):
  91.     #写入文件
  92.                         f.writelines(x+'\r\n')
  93.                         print (f.writelines)
  94.                 else:
  95.                     for x in find_re.findall(html):
  96.     #写入文件
  97.                         f.writelines(x+'\r\n')
  98.                         print (f.writelines)
  99.                
  100. f.close()
  101. print ('download complete!')
  102. #去除重复行
  103. lines_seen = set()
  104. outfile = open("2.txt", "w")
  105. infile = open("zhujia.txt", "r")
  106. lines = infile.readlines()
  107. for line in lines:
  108.     if line not in lines_seen:
  109.        
  110.         outfile.write(line)
  111.         lines_seen.add(line)
  112. outfile.close()
  113. #去除空行
  114. infp = open('2.txt', "r")
  115. outfp = open('1.txt', "w")
  116. lines = infp.readlines()
  117. for lin in lines:
  118.     if lin.split():
  119.         outfp.writelines(li)
  120. infp.close()
  121. outfp.close()
  122. print ('文件排版正常')
3.问题
a.这个网站没限制爬虫,开始没设置请求连接的间隔时间,频繁的循环,貌似让对方服务器崩溃了一会。
b.通过分析HTML,用正则表达式匹配关键数据,而且也可以分析出该网站的数据抄写的习惯。
c.代码中有很多重复的代码,可以用类和函数去写,时间有点不够,没进一步优化代码。
d.写的过程中都是一个一个小功能去写的,小功能能正确输出后,然后进行拼接。
e.还是个小菜鸟所以水平有限,献丑了。
f.有打算把数据用reportlab绘制出PDF的价格走势图。
阅读(1627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~