python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
2024-10-15 05:48:54
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)
最新文章
- 初步认识Node 之Web框架
- JS中exec函数与match函数的区别与联系
- bat操作数据库mysql
- JS 生成GUID
- Android实现资料收藏
- 为cocos2dx添加ndk库
- 201521123042 《Java程序设计》第3周学习总结
- 自己动手实践 spring retry 重试框架
- python读取文本文件数据
- Protocol Buffers(1):序列化、编译与使用
- HighChar 案例
- Lambda查询
- json和pickle模块
- 毕业设计心得与整理-APP-主题切换
- Axure RP for Mac(网站交互式原型设计工具)破解版安装
- Python 深浅copy 和文件操作
- python造数
- selenium自动化测试安装,浏览器驱动版本对应
- PHP学习第一天
- Python运行的方式