参考链接:http://python.jobbole.com/81351/#comment-93968

主要参考自伯乐在线的内容,但是该链接博客下的源码部分的正则表达式部分应该是有问题,试了好几次,没试成功。后来在下面的评论中看到有个使用BeautifulSoup的童鞋,试了试,感觉BeautifulSoup用起来确实很便捷。

 # -*- coding:utf-8 -*-

 '''
Author:LeonWen
''' import urllib
import urllib2
# import re
from bs4 import BeautifulSoup page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
# set the headers
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)
object_bs = BeautifulSoup(response.read())
# print object_bs.prettify()
# items 是一个list保存着返回结果
items = object_bs.body.find_all("div",{"class":"article block untagged mb15"})
# print items
floor = 1
tag = 0
for item in items:
if item.find("div",{"class":"thumb"}) == None:
# class=thumb为带有图片的标签
author = item.find("h2")
upNum = item.find("i",{"class":"number"})
content = item.find("div",{"class":"content"})
# print content.prettify()
# print content.text
print u"===============",floor,u" 楼 ======================="
print u"作者:",author.text
print u"赞同数:",upNum.text
print u"内容:",content.get_text()
floor += 1
else:
tag += 1
print u"图片个数:",tag
except urllib2.URLError,e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason

原文地址:http://www.cnblogs.com/leonwen/p/5721843.html

最新文章

  1. en_windows_10_multiple_editions_version_1511_x64.iso
  2. [异常解决] 初玩SAE遇到的小问题——注册&创建项目+MyEclipse装插件直接部署+一个简单的JSP部署实现
  3. Centos7 Apache 2.4.18编译安装
  4. scrollTop 鼠标往下移动到一定位置显示隐藏
  5. ECharts SSH+JQueryAjax+Json+JSP将数据库中数据填充到ECharts中
  6. C - Courses - hdu 1083(模板)
  7. HDU_2057——64位无符号16进制数的运算
  8. css 实现评分效果
  9. As Easy As A+B
  10. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
  11. wemall app商城源码android开发MD5加密工具类
  12. ES6之Promise
  13. java.sql.SQLException:ORA-01861:文字和格式字符串不匹配
  14. 城市经纬度 json
  15. python小程序--Two
  16. 获取上一页面url
  17. Ajax接收后台发送过来的布尔值以及指定的字符串
  18. socket.io的connect连接时不断的进行自动连接,并产生错误net::ERR_EMPTY_RESPONSE
  19. spring 注解@PathVariable
  20. 尚硅谷STRUTS2视频学习笔记

热门文章

  1. event.stopPropagation()和event.preventDefault()
  2. Linux 部署vue项目(使用nginx)
  3. 配置文件和sqlplus简单使用
  4. [算法]LeetCode 152:乘积最大子序列
  5. .net core的服务器模式和工作站模式
  6. python 安装impala包
  7. Set a One-to-Many Relationship设置一对多关系 (XPO)
  8. HTML常用标签一
  9. Redis中使用redis-cli及密码登录
  10. MES论坛