import urllib2,cookielib
import re,string
import thread,time
class HTTPRefererProcessor(urllib2.BaseHandler):
def __init__(self):
self.referer = None
def http_request(self, request):
if ((self.referer is not None) and
not request.has_header("Referer")):
request.add_unredirected_header("Referer", self.referer)
return request
def http_response(self, request, response):
self.referer = response.geturl()
return response
https_request = http_request
https_response = http_response
def get_page(page_url):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), HTTPRefererProcessor(),)
urllib2.install_opener(opener)
request = urllib2.Request(url = page_url)
ret = opener.open(request)
content = ret.read()
return content
def get_blog(blog_url):
content = get_page(blog_url)
#print content
p = re.compile('<div class="item-content" id="main-content">([\s\S]*?)\<\!\-\-')
m = p.search(content)
if (m):
print m.group(1)
else:
print "fail to open page " + blog_url
def get_blog_urls(start_page, end_page):
#print "trying "+ str(start_page) + "\t" + str( end_page)
for i in range(start_page, end_page ):
base_url = "http://raywill.blog.sohu.com/action/v_frag-ebi_b3797b3792-pg_"+str(i)+"/entry/?o=true"
content = get_page(base_url)
p = re.compile('<h3>[\s\S]*?<a href="([\s\S]*?)" target[\s\S]*?</h3>')
m = p.findall(content)
for elem in m:
print elem
#print "tried "+ str(start_page) + "\t" + str( end_page)
thread.exit_thread()
def get_blog_page_count():
elem="0"
base_url = "http://raywill.blog.sohu.com/entry/"
content = get_page(base_url)
p = re.compile('var totalCount = ([\s\S]*?);')
m = p.findall(content)
for elem in m:
break
return string.atoi(elem)
blog_count = get_blog_page_count()
per_page = 20
pages_per_thread = 4
#get_blog_urls(0, blog_count / per_page)
for r in range(0, blog_count / per_page, pages_per_thread):
thread.start_new_thread(get_blog_urls, (r, r+pages_per_thread))
time.sleep(100)
上面的代码能够得到搜狐博客的全部文章列表,为了加快爬取速度,还使用了多线程。以上仅仅为原型系统,下一篇将对上一篇文章和这一篇文章进行一个汇总和工程化,形成一个完整的搜狐博客网络蜘蛛。ENjoY~
爬取结果举例:
http://raywill.blog.sohu.com/136257838.html
http://raywill.blog.sohu.com/136085482.html
http://raywill.blog.sohu.com/135681113.html
http://raywill.blog.sohu.com/135307188.html
http://raywill.blog.sohu.com/135117405.html
http://raywill.blog.sohu.com/134659680.html
http://raywill.blog.sohu.com/134260278.html
http://raywill.blog.sohu.com/132457980.html
http://raywill.blog.sohu.com/131040360.html
http://raywill.blog.sohu.com/130786144.html
http://raywill.blog.sohu.com/130723867.html
http://raywill.blog.sohu.com/130360431.html
http://raywill.blog.sohu.com/130252301.html
http://raywill.blog.sohu.com/129862227.html
http://raywill.blog.sohu.com/129684019.html
对用到的几个python知识点进行一下小结:
1. for只有一种模式:for x in y
2. range函数左闭右开,第三个参数可选,表示步长
3. string支持将字符串转化成整数或浮点:string.atoi/atof
4. 在爬取网页时,好像线程过多的话会引起爬取错误(部分线程抛出异常)
上一篇:
作者邮箱: hustos (a) qq.com
作者微博: weibo.com/raywill2
分享到:
相关推荐
商剑分布式网络蜘蛛,性能高速运转,能耗尽全部带宽,可批量采集海量数据的网页,若几百台服务器安装商剑...更是搜索引擎-网络蜘蛛-网络爬虫-spider-网页抓取等技术的必备工具之一。http://www.100spider.cn/wspider.rar
抢先式多线程网络蜘蛛-VC源代码 抢先式多线程网络蜘蛛-VC源代码
一个简单的网络蜘蛛Java源码,是用Applet实现的,所以压缩包中也包含了调用此Applet的html文件。只需编译此java源代码用此html文件调用即可。
*处理速度快,如果网络畅通,1小时可以抓取、解析10000个网页 *采用独特的重复数据过滤技术,支持增量式数据抓取,可以抓取实时数据,如:股票交易信息、天气预报等 *抓取信息的准确性高,系统提供了强大的数据...
互联网一定是蜘蛛的天下,摧毁一切的力量就来源于蜘蛛~
网络蜘蛛,自动抓取网上图片,数据库SQLServer,
网络蜘蛛网络蜘蛛网络蜘蛛网络蜘蛛网络蜘蛛网络蜘蛛
VB写的网络蜘蛛程序,可以批量采集互联网上的网站,VB写的网络蜘蛛程序,可以批量采集互联网上的网站
智者百度蜘蛛王(链接批量推送)是一款百度链接推送软件,通过百度蜘蛛王(链接批量推送)您可以将大量的链接推送给百度蜘蛛进行抓取,目前软件支持主动推送和自动推送两种。 1、主动推送:通过百度token进行批量推送...
网络蜘蛛 网络蜘蛛 网络蜘蛛 网络蜘蛛 网络爬虫
c# 网络蜘蛛 下载图片源代码 c# 网络蜘蛛 下载图片源代码
蜘蛛-低多边形-动画 游戏开发动画 低多边形蜘蛛模型 1552个三角形、5个纹理、 分辨率为2048 x 2048 jpg + 法线贴图 FBX、OBJ、C4D (R13), 提供已分配动画的Unity项目文件 15种动画:0-45 = 走路 46-65 = 攻击1 ...
针对51job招聘信息抓取,开启多个进程,与多线程抓取,子进程通知主进程抓取状态。使用MySql数据库
Java网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_zhizhuJava网络爬虫(蜘蛛)源码_...
百度批量提交工具可以增加蜘蛛爬行内置了代理验证功能,更适合长期使用。
带拼音的儿歌--落叶-大白鹅-小蜘蛛-石拱桥-粗心的小画家-蒲公英.doc
可配置:线程数、线程等待时间,连接超时时间,可爬取文件类型和优先级、下载目录等。 状态栏显示统计信息:排入队列URL数,已下载文件数,已下载总字节数,CPU使用率和可用内存等。 有偏好的爬虫:可针对爬取的资源...
C#-Spider网络爬虫网络机器人网络蜘蛛多线程下载HTTP协议[归类].pdf
D2B2C电商平台功能模块方案书-四川红蜘蛛-XXXX623.pptx