#_*_coding:utf-8-*-
import urllib2
import traceback
import codecs
from BeautifulSoup import BeautifulSoup def openSoup(url,code):
page = urllib2.urlopen(url)
soup = BeautifulSoup(page,fromEncoding=code)#,fromEncoding="gb2312"
#soup = BeautifulSoup(page,code)
return soup def getContentFromDiv(contents):
s = ""
for content in contents:
try:
s += content
except:
pass s = s.lstrip().rstrip()
if len(s) < 50:
return ""
else:
return " "+s+"\r\n"+"\r\n" def readHtml(soup,fp,authname):
pageContent = ""
item = soup.find(name='div', attrs={'class':'bbs-content clearfix'})
if item != None:
pageContent += getContentFromDiv(item.contents) items = soup.findAll(name='div', attrs={'class':'atl-item'})
for item in items:
userItem = item.find(name='a', attrs={'class':'js-vip-check'})
if userItem == None or userItem.contents[0] != authname:
continue contentItem = item.find(name='div', attrs={'class':'bbs-content'})
pageContent += getContentFromDiv(contentItem.contents) fp.write(pageContent) def getNextPage(soup,pno):
nextlink = soup.find(name="a",attrs={"class":"js-keyboard-next"})
if nextlink != None:
return "http://bbs.tianya.cn"+nextlink["href"]
else:
return 'OVER' def getHtml(url,filename,authname):
p = 1
fp = codecs.open(filename,'w','utf-8')
while True:
soup = openSoup(url,'utf-8')
readHtml(soup,fp,authname)
url = getNextPage(soup,p+1)
if url == 'OVER' :
break
print 'PAGE '+str(p)+' OK'
p = p + 1 print 'It\'s Over'
fp.close() if __name__ == '__main__':
getHtml('http://bbs.tianya.cn/post-no05-143258-1.shtml','krzc.txt',u'关河五十州')
#getHtml('http://bbs.tianya.cn/post-no05-143258-1036.shtml','krzc.txt',u'关河五十州')

抓取天涯文章的蜘蛛代码,刚经过更新(因为天涯页面HTML代码变化)的更多相关文章

  1. 基于Node.js的强大爬虫 能直接发布抓取的文章哦

    基于Node.js的强大爬虫 能直接发布抓取的文章哦 基于Node.js的强大爬虫能直接发布抓取的文章哦!本爬虫源码基于WTFPL协议,感兴趣的小伙伴们可以参考一下 一.环境配置 1)搞一台服务器,什 ...

  2. [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章

    借助搜索微信搜索引擎进行抓取 抓取过程 1.首先在搜狗的微信搜索页面测试一下,这样能够让我们的思路更加清晰 在搜索引擎上使用微信公众号英文名进行“搜公众号”操作(因为公众号英文名是公众号唯一的,而中文 ...

  3. 使用redis所维护的代理池抓取微信文章

    搜狗搜索可以直接搜索微信文章,本次就是利用搜狗搜搜出微信文章,获得详细的文章url来得到文章的信息.并把我们感兴趣的内容存入到mongodb中. 因为搜狗搜索微信文章的反爬虫比较强,经常封IP,所以要 ...

  4. python抓取头条文章

    python抓取头条美文并存储到mongodb # Author:song from multiprocessing import Pool from urllib.parse import urle ...

  5. asp.net mvc抓取微信文章里面所有的图片

    /// <summary> /// 下载指定URL下的所有图片 /// </summary> public class WebPageImage { /// <summa ...

  6. asp.net MVC 抓取微信文章数据(正文)

    1.抓微信的正文主要是调用第三方的接口(https://market.aliyun.com/products/56928004/cmapi012134.html) using Newtonsoft.J ...

  7. 代理池抓取基础版-(python协程)--抓取网站(西刺-后期会持续更新)

    # coding = utf- __autor__ = 'litao' import urllib.request import urllib.request import urllib.error ...

  8. 【转】Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  9. Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

随机推荐

  1. IOS关于XIB文件和调试时候显示不一样问题

    1 前言 今天工作中,遇到了一个xib文件布局问题,具体问题如下:在xib中加了一个图片,背景为已经切好的图片,但是当显示在模拟器上面的时候却显示不出来效果. 2 详述 2.1 问题截图      如 ...

  2. Bootstrap-模态框Modal使用

    传值使用JavaScript方式吧,代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  3. 80端口被system占用的问题

    80端口被system占用的问题今天启动Apache的时候发现无法启动-80端口被占用. 检查进程发现是system进程id=4给占用了.这个是系统进程 检查服务,看看IIS.没发现问题. 检查了半天 ...

  4. Android源代码之Gallery专题研究(1)

    前言 时光飞逝,从事Android系统开发已经两年了,总想写点什么来安慰自己.思考了非常久总是无法下笔,认为没什么好写的.如今最终决定写一些符合大多数人需求的东西,想必使用过Android手机的人们一 ...

  5. [Webpack 2] Tree shaking with Webpack 2

    The less code you can send to the browser, the better. The concept of tree shaking basically says th ...

  6. Android_设备隐私获取,忽略6.0权限管理

    1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...

  7. 追踪CPU跑满 堆栈调试

    http://blog.donghao.org/2014/04/24/%E8%BF%BD%E8%B8%AAcpu%E8%B7%91%E6%BB%A1/

  8. win2003 安装itunes ----iphone4s

    工具包下载:http://download.csdn.net/detail/trassion/5852663 在windows 2003 上安装itunes 8.2.0.10 ,会提示 AppleMo ...

  9. java web 一次请求从开始到响应结束的过程

    博客原文:   http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html   HTTP(HyperText Transfer ...

  10. CHENEY-YANG'S BLOG(cheney-yang)

    This is Cheney-Yang's blog ! Welcome to here ! This is Cheney-Yang's blog ! Welcome to here ! This i ...