url可以在网页迅速打开 但urllib2.urlopen(url,timeout=xx) 却超时了

Ray_20 2014-03-31 09:28:44


#! /usr/bin/python
#coding=utf-8

import urllib2
import urllib
from sys import argv # get data from command line
import HTMLParser # parse the data which spider get
import re
import redis
import string
import datetime


class SpiderRegular(object):

def __init__(self, pattern , string):
self.pattern = pattern
self.string = string

def getnexturl(self):

sub = re.search(self.pattern, self.string)
if not sub:
return None
return re.search(r'".+\.html"', sub.group()).group()

def inredis(self):

try:
redis_conn = redis.Redis(host='127.0.0.1', port=6379)
redis_conn.set('nexturl', self.getnexturl())
except redis.RedisError, e:
raise(e)

def outredis(self):

self.inredis()

try:
redis_conn = redis.Redis(host='127.0.0.1', port=6379)
nexurl = redis_conn.get('nexturl')
except redis.RedisError, e:
raise(e)
else:
return nexurl




class Spider(object):

def __init__(self, baseurl):

self.nexturl = baseurl


def getindexpage(self):
self.html = ''
try:
htmlobj = urllib2.urlopen(self.nexturl, timeout=350)
self.html = htmlobj.read()
except Exception, e:
raise e
else:
htmlobj.close()
return self.html


def geturls(self):

self.indexpage = self.getindexpage()
urls = {}

if self.indexpage:
nextpath = SpiderRegular(r'<LINK HREF=".+" REL=next>', self.indexpage).getnexturl()
if nextpath:
self.nexturl = self.nexturl[:self.nexturl.rfind('/')+1] + nextpath.strip('"')
print 'get url: %s' % self.nexturl
urls[nextpath.strip('"')] = self.nexturl
print '[%s]: set url {%s} to dict' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), self.nexturl)
self.geturls()
else:
return urls
return urls

def action(self, path):

urls = self.geturls()
print '[%s]: get urls success . . .' % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for url in urls:
urllib.urlretrieve(url, path + urls[url])

print '[%s]: Download success' % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')


def main():
spider = Spider('http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html')
spider.action('/home/yfsuse/pyproject/pyeah/pyspider/howtobash/')


if __name__ =='__main__':
main()

...全文
463 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsjfdjoijvtghu 2014-04-02
  • 打赏
  • 举报
回复
在交互界面urlopen试试快不快
还是不快的话,python的urlopen有时的确匪夷所思,比如同样内容python发出去就是没响应

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧