python学习记——爬糗事百科
2015-11-08 16:18
645 查看
学习python 跟着教程写了一个爬糗事百科的段子程序(去掉了包括图片的段子)
代码中item[2]为空表示段子中不带图片
运行结果如下:
代码如下:
参考教程:http://cuiqingcai.com/990.html
代码中item[2]为空表示段子中不带图片
运行结果如下:
代码如下:
# -*- coding:utf-8 -*- import urllib import urllib2 import re page = 1 url = 'http://www.qiushibaike.com/hot/page/' + str(page) user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36' headers = { 'User-Agent' : user_agent } try: request = urllib2.Request(url,headers = headers) response = urllib2.urlopen(request) content = response.read().decode('utf-8') pattern = re.compile('<div class="author clearfix">.*?<a.*?>.*?<img.*?>.*?</a>.*?<a.*?>.*?<h2>(.*?)</h2>.*?</a>.*?</div>.*?<div class="content">(.*?)<!--.*?-->.*?</div>(.*?)<div class="stats">',re.S) items = re.findall(pattern,content) for item in items: haveImg = re.search("img",item[2]) if not haveImg: print item[0],item[1] except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reason
参考教程:http://cuiqingcai.com/990.html
相关文章推荐
- NumPy-快速处理数据--ufunc运算--广播--ufunc方法
- 《用Python玩转数据》第1周学习笔记(Part 1)
- Python中定义字符串和修改字符串的原理
- 【重要】python之模块CGI 通用网关接口
- Python pickle 的 dump() & load()
- python之模块calendar(汇集了日历相关的操作)
- python列表相乘函数map函数
- python之模块base64
- 为Python添加默认模块搜索路径
- Python __name__ 和 __main__
- [Python] Pandas 学习笔记(一)
- python 基础心得
- 在anaconda python开发套件下进行opencv的安装
- Python实现模拟时钟代码推荐
- 11.8笔记python webpy优雅的设置静态文件路径,macos防止休眠
- Python学习笔记(3)range的用法
- [转]python os模块 常用命令
- Python入门篇之函数
- Python 基本类型转换
- Quant 应该学习哪些 Python 知识?