python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/

最终输出结果格式如:2015075期开奖号码:6,11,13,19,21,32, 蓝球:4

直接用python源码写的抓取双色球最新开奖数据的代码,没使用框架,直接用字符串截取的方式写的,经过测试速度还是很快的

使用pyspider可以轻松分析出需要的内容,不过需要部署框架对只抓取特定内容的小应用来说也没多大必要
一般的抓取网页的使用 beautifulsoup就足够了,pyspider真正做爬虫类的应用才需要用到

python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容 - 流风,飘然的风 - 博客园
http://www.cnblogs.com/zdz8207/p/python_learn_note_17.html

使用BeautifulSoup4对比直接使用字符串查找截取的方式要更加直观和简洁。

把代码作为开源项目了,热血狂徒 / zyspider - 代码托管 - 开源中国社区
http://git.oschina.net/coos/zyspider

====================================

 import urllib.request
import urllib.parse
import re
import urllib.request,urllib.parse,http.cookiejar def getHtml(url):
cj=http.cookiejar.CookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'),('Cookie','')] urllib.request.install_opener(opener) html_bytes = urllib.request.urlopen( url ).read()
html_string = html_bytes.decode( 'utf-8' )
return html_string #url = http://zst.aicai.com/ssq/openInfo/
#最终输出结果格式如:2015075期开奖号码:6,11,13,19,21,32, 蓝球:4
html = getHtml("http://zst.aicai.com/ssq/openInfo/")
#<table class="fzTab nbt"> </table> table = html[html.find('<table class="fzTab nbt">') : html.find('</table>')]
#print (table)
#<tr onmouseout="this.style.background=''" onmouseover="this.style.background='#fff7d8'">
#<tr \r\n\t\t onmouseout=
tmp = table.split('<tr \r\n\t\t onmouseout=',1)
#print(tmp)
#print(len(tmp))
trs = tmp[1]
tr = trs[: trs.find('</tr>')]
#print(tr)
number = tr.split('<td >')[1].split('</td>')[0]
print(number + '期开奖号码:',end='')
redtmp = tr.split('<td class="redColor sz12" >')
reds = redtmp[1:len(redtmp)-1]#去掉第一个和最后一个没用的元素
#print(reds)
for redstr in reds:
print(redstr.split('</td>')[0] + ",",end='')
print('蓝球:',end='')
blue = tr.split('<td class="blueColor sz12" >')[1].split('</td>')[0]
print(blue)

最新文章

  1. 初步认识Node 之Web框架
  2. JS中exec函数与match函数的区别与联系
  3. bat操作数据库mysql
  4. JS 生成GUID
  5. Android实现资料收藏
  6. 为cocos2dx添加ndk库
  7. 201521123042 《Java程序设计》第3周学习总结
  8. 自己动手实践 spring retry 重试框架
  9. python读取文本文件数据
  10. Protocol Buffers(1):序列化、编译与使用
  11. HighChar 案例
  12. Lambda查询
  13. json和pickle模块
  14. 毕业设计心得与整理-APP-主题切换
  15. Axure RP for Mac(网站交互式原型设计工具)破解版安装
  16. Python 深浅copy 和文件操作
  17. python造数
  18. selenium自动化测试安装,浏览器驱动版本对应
  19. PHP学习第一天
  20. Python运行的方式

热门文章

  1. [LintCode] Toy Factory 玩具工厂
  2. Unique Binary Search Trees
  3. unity3d插件Daikon Forge GUI 中文教程1-Daikon Forge介绍
  4. 导出所选行为excle
  5. js控制滚动条平滑滚动到制定位置
  6. php中英字符串截取
  7. time模块目录下自己建立一个名为log的文件夹
  8. PHP版本VC6与VC9、Thread Safe与None-Thread Safe等的区别
  9. laravel 4.2 session
  10. Yii源码阅读笔记(二十六)