一天一爬虫:day01-糗事百科_v1

使用python2,Requests和re,爬取糗事百科,版本一,实现能够爬取发布人,笑话内容,去除有图片的笑话好笑数,评论数,up数,down数(up数-down数=好笑数)


#-*—coding:utf8-*-
#糗事百科,发布人,笑话内容,去除有图片的笑话,
#好笑数,评论数,up数,down数(up数-down数=好笑数)
import requestsimport re#下面三行是编码转换的功能import sysreload(sys)sys.setdefaultencoding("utf-8")#hea是我们自己构造的一个字典,里面保存了user-agenthea = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'}html = requests.get('http://www.qiushibaike.com/hot/page/3',headers = hea)html.encoding = 'utf-8' #这一行是将编码转为utf-8否则中文会显示乱码。story = re.findall('<h2>(.*?)</h2>.*?<div class="content">.*?<span>(.*?)</span>.*?</div>(.*?)<div class="stats">'+ '.*?<i class="number">(.*?)</i> 好笑.*?class="number">(.*?)</i> 评论'+ '.*?class="up">.*?class="number hidden">(.*?)</span>.*?class="down">.*?class="number hidden">(.*?)</span>',html.text,re.S)###### (.*?)处不能为空; 每个(.*?)对应一个元素,依次为:# 发布者,内容,图片链接,好笑数,评论数,up数,down数(up数-down数=好笑数)count=1for item in story: haveImg = re.search("img",item[2]) if not haveImg: replaceBR = re.compile('<br/>') text = re.sub(replaceBR, "\n", item[1]) print count,item[0] print text print item[5]+'up'+'\t'+item[6]+'down'+'\t'+item[3]+'好笑'+'\t'+item[4]+'评论' print "*****************" count=count+1



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值