python爬虫

目的:想利用python进行爬虫,将爬虫的结果保存到本地,然后在利用js提取数据呈现在html中。

第一步:爬虫

http://python.jobbole.com/81351/

从网上找到的一片有关于python的爬虫文章。

这篇文章说是要查找用户名,内容,时间,评论的人数,但是在进行正则表达是进行筛选的时候有错误。这里就不在进行更正了。我的目的是进行爬虫,将爬虫的结果保存到本地,然后利用js调用此数据呈现在html中。  

import urllib
import urllib2
import re
 
page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
    request = urllib2.Request(url,headers = headers)
    response = urllib2.urlopen(request)
    print response.read()
    content = response.read().decode('utf-8')
    pattern = re.compile('<div.*?class="author.*?>.*?<a.*?</a>.*?<a.*?>(.*?)</a>.*?<div.*?class'+
                         '="content".*?title="(.*?)">(.*?)</div>(.*?)<div class="stats.*?class="number">(.*?)</i>',re.S)
    items = re.findall(pattern,content)
    #for item in items:
        #haveImg = re.search("img",item[3])
        #if not haveImg:
            #print item[0],item[1],item[2],item[4]
        #print item[4]
except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason

用此代码爬虫的结果print item[1,2,3,4]分别是

<h2>Hao166</h2>

<h2>宝贝别怕,我在这</h2>

<h2>请叫我胡太太~</h2>

<h2>北叶枫</h2>

<h2>逆风的单车</h2>

<h2>葉孤城`</h2>

<h2>鸡鸡炖冬瓜</h2>

<h2>一晖斜阳</h2>

<h2>真想甩你一巴掌</h2>

<h2>玛莎拉依蒂</h2>

>>> ======================== RESTART: C:/Python27/pyt.py ======================== 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow 分享到微信" rel="nofollow >>> ======================== RESTART: C:/Python27/pyt.py ======================== 微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

微信</a> <a class="share-qq" data-type="qq" title="分享到QQ" rel="nofollow">QQ</a> <a class="share-qzone" data-type="qzone" title="分享到QQ空间" rel="nofollow">QQ空间</a> <a class="share-weibo" data-type="weibo" title="分享到微博" rel="nofollow">微博</a>

>>> ======================== RESTART: C:/Python27/pyt.py ========================

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116909278'>

<div class="author clearfix"> <a href="/users/32126062/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/3212/32126062/medium/20160707001649.jpg" alt="宝贝别怕,我在这"/> </a> <a href="/users/32126062/" target="_blank" title="宝贝别怕,我在这"> <h2>宝贝别怕,我在这</h2> </a> </div>

<div class="content">

昨天吃完晚饭,我妈对我说:走出去遛狗去,我惊讶的看着妈妈说,家里买狗了?我妈用镇定的眼睛看着我,好吧,我懂了。

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116910168'>

<div class="author clearfix"> <a href="/users/28505156/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/2850/28505156/medium/20160606162255.jpg" alt="葉孤城`"/> </a> <a href="/users/28505156/" target="_blank" title="葉孤城`"> <h2>葉孤城`</h2> </a> </div>

<div class="content">

读书的时候喜欢一个女孩,于是每天故意经过她家,盯着她家看,希望能一睹芳颜。直到有一天,她老爸出现说:小伙子,我注意你很久了,是不是想偷我们家桔子?<br/>于是…我不敢去了!

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116899441'>

<div class="author clearfix"> <a href="/users/27813208/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/2781/27813208/medium/20150421161337.jpg" alt="小猪不美丽"/> </a> <a href="/users/27813208/" target="_blank" title="小猪不美丽"> <h2>小猪不美丽</h2> </a> </div>

<div class="content">

我有一个160CM高的大熊,今天实在没衣服穿,发现他身上的T恤不错,就扒下来穿了。。。,婆婆给我打电话说:家里好像来了个变态贼,大熊的衣服都被扒光了。。。

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116909255'>

<div class="author clearfix"> <a href="/users/30839416/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/3083/30839416/medium/20151224135712.jpg" alt="北叶枫"/> </a> <a href="/users/30839416/" target="_blank" title="北叶枫"> <h2>北叶枫</h2> </a> </div>

<div class="content">

今天去培训  投影布突然掉了   突然听到有人叫  老师射墙上了  。。。老师射墙上了。当时笑声一篇。。

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116909729'>

<div class="author clearfix"> <a href="/users/30591370/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/3059/30591370/medium/20160506204149.jpg" alt="逆风的单车"/> </a> <a href="/users/30591370/" target="_blank" title="逆风的单车"> <h2>逆风的单车</h2> </a> </div>

<div class="content">

在外吃午饭,一妹子对老板说“给我来两份炒饭,一大一小,大的打包,小的不要太热,我和我男友在你家吃的第一顿饭,就是炒饭,现在他劈腿了,一会他来了,小的我用来扣他脸上!!!!”<br/>卧槽!!!。。。。。

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116896846'>

<div class="author clearfix"> <a href="/users/25747512/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/2574/25747512/medium/20160531205552.jpg" alt="告诉静静说我想她"/> </a> <a href="/users/25747512/" target="_blank" title="告诉静静说我想她"> <h2>告诉静静说我想她</h2> </a> </div>

<div class="content">

我和我爸长得很像,大家都说一个模子刻出来的。<br/>但是也有很多的困扰,比如我妈跟我爸吵过架以后,老妈一看到我和爸那相似的脸,想想又来气,尼玛冲我又发通火……

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116907244'>

<div class="author clearfix"> <a href="/users/10783511/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/1078/10783511/medium/20130909133101.jpg" alt="鸡鸡炖冬瓜"/> </a> <a href="/users/10783511/" target="_blank" title="鸡鸡炖冬瓜"> <h2>鸡鸡炖冬瓜</h2> </a> </div>

<div class="content">

昨晚做梦,梦到在学校上课,妈的老师点名让我回答问题,正在支支吾吾的时候突然被尿憋醒,那个高兴哇。。等我上了厕所又睡着了,又梦到那个老师了,“同学,把刚才的问题回答下”,我。。。。。。

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116908367'>

<div class="author clearfix"> <a href="/users/31721625/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/3172/31721625/medium/20160503205527.jpg" alt="一晖斜阳"/> </a> <a href="/users/31721625/" target="_blank" title="一晖斜阳"> <h2>一晖斜阳</h2> </a> </div>

<div class="content">

隔壁寝的人不喜欢带钥匙,就把一把钥匙放我们寝,我们寝的也不常带,就我一直带着,今天考试我交的很晚,他们早就回来了,然而并没啥卵用。还是得等我,忽然感觉到满满的存在感!哈哈哈!

</div>

 

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116898998'>

<div class="author clearfix"> <a href="/users/21333057/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/2133/21333057/medium/20160706092841.jpg" alt="卖蘑菇的姑娘"/> </a> <a href="/users/21333057/" target="_blank" title="卖蘑菇的姑娘"> <h2>卖蘑菇的姑娘</h2> </a> </div>

<div class="content">

老板打电话问怎么还没到公司,天气再不好路上的水再多我们也要时刻有一颗抗洪救灾的责任心,赶紧来上班,就差你们几个了!<br/>等我爬山涉水的赶到公司,尼玛公司就来了七八个人,都光着脚丫子一脸蒙逼的看着我,尼玛!<br/>我问老板来了吗?<br/>一主管说老板的车半路熄火了,估计现在还在等救援!

</div>

 

<div class="thumb">

<a href="/article/116898998" target="_blank"> <img src="http://pic.qiushibaike.com/system/pictures/11689/116898998/medium/app116898998.jpg" alt="尼玛公司就来了七八个人" /> </a>

</div>

 

<div class="single-clear"></div>

</div>  

 

 

<div class="article block untagged mb15" id='qiushi_tag_116909084'>

<div class="author clearfix"> <a href="/users/31445295/" target="_blank" rel="nofollow"> <img src="http://pic.qiushibaike.com/system/avtnew/3144/31445295/medium/20160621165127.jpg" alt="耶和华神"/> </a> <a href="/users/31445295/" target="_blank" title="耶和华神"> <h2>耶和华神</h2> </a> </div>

<div class="content">

听砖家说,人若是每天睡8小时,那么,人生的三分之一就浪费掉了。幸好,我是个学生狗,每天只睡5、6个小时,如此说来,我赚大了!

</div>

 

>>> ======================== RESTART: C:/Python27/pyt.py ======================== 2023 2779 17208 3698 3662 10174 3992 4159 369 1886 >>>

保存到本地

小插曲----想文件内写入内容

for j in range(0,9):
    f = open('hello.txt','a+')
    f.write(str(j))
    f.close()

第二步:利用js进行提取显示到前端html

介绍一个插件echarts

http://echarts.baidu.com/echarts2/doc/start.html----经验证可以实现如图上所说的内容

http://echarts.baidu.com/echarts2/doc/example.html----Echarts实例

http://echarts.baidu.com/echarts2/doc/doc.html----echarts中各个模块(柱形图或是圆饼图等的模块)

在echarts中引用表格是个人认为简化为以下几步

第一:在html中占一个位置

第二:引入模块化单文件echarts.js

第三:设置路径

第四:撰写选取呈现函数   

 

posted on 2016-07-08 23:28  蜗Amazon牛  阅读(132)  评论(0)    收藏  举报