获取糗事百科段子内容(根据网络代码改编)
2016-08-29 16:02
316 查看
最近在学习获取网络数据,在网上看到一个获取获取糗事百科段子内容的实例(点击打开链接)
因为糗事百科的版本变化了所以原代码不可以直接使用,在评论中有可直接使用的代码,下面的代码是我根据博主的源码改编而成的,思路和方法可以看博主原文
# encoding: utf-8
import urllib2
import re
class Qsbk():
def __init__(self):
self.url = 'http://www.qiushibaike.com/hot/page/'
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
# 初始化headers
self.headers = {'User-Agent': self.user_agent}
def html_data(self, pageIndex):
try:
url = self.url + str(pageIndex)
# 构建请求的request
request = urllib2.Request(url, headers=self.headers)
# 利用urlopen获取页面代码
response = urllib2.urlopen(request)
# 将页面转化为UTF-8编码
pageCode = response.read().decode('utf-8')
# print pageCode
return pageCode
except urllib2.URLError, e:
if hasattr(e, "reason"):
print u"连接糗事百科失败,错误原因", e.reason
return None
def data_split(self, pageIndex):
pageCode = self.html_data(pageIndex)
if not pageCode:
print "页面加载失败...."
return None
pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?</div>.*?'
'<div class="content">(.*?)</div>.*?'
'<i class="number">(.*?)</i>.*?<i class="number">(.*?)</i>', re.S)
items = pattern.findall(pageCode)
return list(items)
def out_put(self):
i = 1
j = 0
output = ''
datats = self.data_split(i)
while (j < len(datats)):
print "正在读取糗事百科,按回车查看新段子,q退出"
output = raw_input()
if output == '':
print '第' + str(i) + '页'
print ''.join(datats[j])
j += 1
if(j == len(datats)):
i += 1
j = 0
datats = self.data_split(i)
elif output == 'q':
break
else:
print "正在读取糗事百科,按回车查看新段子,Q退出"
output = raw_input()
if __name__ == '__main__':
Qsbk().out_put()
因为糗事百科的版本变化了所以原代码不可以直接使用,在评论中有可直接使用的代码,下面的代码是我根据博主的源码改编而成的,思路和方法可以看博主原文
# encoding: utf-8
import urllib2
import re
class Qsbk():
def __init__(self):
self.url = 'http://www.qiushibaike.com/hot/page/'
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
# 初始化headers
self.headers = {'User-Agent': self.user_agent}
def html_data(self, pageIndex):
try:
url = self.url + str(pageIndex)
# 构建请求的request
request = urllib2.Request(url, headers=self.headers)
# 利用urlopen获取页面代码
response = urllib2.urlopen(request)
# 将页面转化为UTF-8编码
pageCode = response.read().decode('utf-8')
# print pageCode
return pageCode
except urllib2.URLError, e:
if hasattr(e, "reason"):
print u"连接糗事百科失败,错误原因", e.reason
return None
def data_split(self, pageIndex):
pageCode = self.html_data(pageIndex)
if not pageCode:
print "页面加载失败...."
return None
pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?</div>.*?'
'<div class="content">(.*?)</div>.*?'
'<i class="number">(.*?)</i>.*?<i class="number">(.*?)</i>', re.S)
items = pattern.findall(pageCode)
return list(items)
def out_put(self):
i = 1
j = 0
output = ''
datats = self.data_split(i)
while (j < len(datats)):
print "正在读取糗事百科,按回车查看新段子,q退出"
output = raw_input()
if output == '':
print '第' + str(i) + '页'
print ''.join(datats[j])
j += 1
if(j == len(datats)):
i += 1
j = 0
datats = self.data_split(i)
elif output == 'q':
break
else:
print "正在读取糗事百科,按回车查看新段子,Q退出"
output = raw_input()
if __name__ == '__main__':
Qsbk().out_put()
相关文章推荐
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- JAVA根据模板将动态内容生成静态网页的代码
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- javascript 根据歌名获取播放地址和歌词内容
- javascript 根据歌名获取播放地址和歌词内容
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- VB.net实现从ListView控件中异地获取文本内容源代码
- XMLHttp ASP远程获取网页内容代码
- GridView根据获取的值显示不同内容
- 从网络获取某些内容!!!
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- 关于根据后台代码获取confirm返回结果后再执行后台代码(转)
- 关于根据后台代码获取confirm返回结果后再执行后台代码
- 根据取到文件的内容 生成二进制文件 并通过servlet获取
- 获取网络中RSS频道内容
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- 实例分析之用ASP编程实现网络内容快速查找的代码
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- FCK常用Js,获取FCK内容,统计FCK字数,向FCK写入指定代码
- SQL 根据汉字获取全拼的代码