#_*_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. 草稿-Hyper-V

    Hyper-V Over SMB3.0 为Hyper-v宿主机和故障转移群集做防病毒排除 微软SMB 3.0文件共享协议新特性介绍

  2. [Redux] Fetching Data on Route Change

    We will learn how to fire up an async request when the route changes. A mock server data: /** /api/i ...

  3. Java IO流学习总结(转)

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  4. 网络学习笔记----01--pathping跟踪数据包路径

    操作系统win7 Pathping主要用于提供有关在来源和目标之间的中间跃点处的网络滞后和网络丢失的信息. Pathping将多个回显请求消息发送到来源和目标之间的各个路由器一段时间,然后根据各个路由 ...

  5. VS2010项目转化为VS2008项目

    第一步: 打开VS2010项目的SLN文件有如下代码: Microsoft Visual Studio Solution File, Format Version 11.00# Visual Stud ...

  6. Android(java)学习笔记186:对ListView等列表组件中数据进行增、删、改操作

    1.ListView介绍 解决大量的相似的数据显示问题 采用了MVC模式: M: model (数据模型) V:  view  (显示的视图) C: controller 控制器 入门案例: acit ...

  7. PHP中的strtotime()对于31日求上个月有问题

    原文出处 <?php $date = "2012-07-31"; $date_unix = strtotime($date); $lastmonth = strtotime( ...

  8. js获取元素transform参数得出的个人理解

    之前写页面的时候有试过想用js获取某些元素的translate的数值什么的,但是translate又是transform的子样式(勉强说说),理所当然就是先获取transform样式,再读里面的值. ...

  9. git 分布式版本控制了解

    今天也来了解一下这个版本控制神器,下面了解一些词语的意思 先说集中式版本系统,版本库是集中放在中央服务器的,干活的时候,都是用自己的电脑,从中央处理器取得最新的版本,干完活后,在把自己的活推送给服务器 ...

  10. php strtotime函数服务器和本地不相同

    遇到过一种情况strtotime 在本地和服务器不相同 服务器返回的是-1 strtotime($sa_sagyo_ymd."23:59:59") 如果这样用不了,就只能换一种写法 ...