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

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)
上一篇:机器学习--降维算法:PCA主成分分析


下一篇:C# 操作Excel数据透视表