您的位置:首页 > 编程语言 > Python开发

python学习记——爬糗事百科

2015-11-08 16:18 645 查看
学习python 跟着教程写了一个爬糗事百科的段子程序(去掉了包括图片的段子)

代码中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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: