python2.4升级2.7.2入门实例
内容摘要
这篇文章主要为大家详细介绍了python2.4升级2.7.2入门实例,具有一定的参考价值,可以用来参考一下。
对python这个高级语言对此感兴趣的朋友,看看idc笔记做的技术笔记!
[root@~]
对python这个高级语言对此感兴趣的朋友,看看idc笔记做的技术笔记!
[root@~]
文章正文
这篇文章主要为大家详细介绍了python2.4升级2.7.2入门实例,具有一定的参考价值,可以用来参考一下。
对python这个高级语言对此感兴趣的朋友,看看idc笔记做的技术笔记![root@~]# pythonPython 2.4.3 (#1, May 5 2011, 16:39:10)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2Type "help", "copyright", "credits" or "license" for more information.>;>>[root@~]#
下载新版本的python
[root@~]# wget
解压缩 以及编译
[root@~]# tar jxvf Python-2.7.3.tar.bz2[root@wangyuelou Python-2.7.2]# ./configure --prefix=/usr/local/python27[root@wangyuelou Python-2.7.2]# make[root@wangyuelou Python-2.7.2]# make install[root@wangyuelou Python-2.7.2]# ls /usr/local/python27/ -altotal 28drwxr-xr-x 6 root root 4096 Jul 14 00:21 .drwxr-xr-x 20 root root 4096 Jul 14 00:17 ..drwxr-xr-x 2 root root 4096 Jul 14 00:21 bindrwxr-xr-x 3 root root 4096 Jul 14 00:21 includedrwxr-xr-x 4 root root 4096 Jul 14 00:21 libdrwxr-xr-x 3 root root 4096 Jul 14 00:21 share
覆盖原来的python链接
[root@wangyuelou Python-2.7.2]# mv /usr/bin/python /usr/bin/python_old[root@wangyuelou Python-2.7.2]# ln -s /usr/local/python27/bin/python /usr/bin/[root@wangyuelou Python-2.7.2]# pythonPython 2.7.2 (default, Jul 14 2011, 00:20:14)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
此处已经可以正常使用python2.7了
但是因为yum是使用的2.4的版本来用的,所以 还需要修改一下
[root@~]# yumThere was a problem importing one of the Python modulesrequired to run yum. The error leading to this problem was:No module named yumPlease install a package which provides this module, orverify that the module is installed correctly.It's possible that the above module doesn't match thecurrent version of Python, which is:2.7.2 (default, Jul 14 2011, 00:20:14)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)]If you cannot solve this problem yourself, please go tothe yum faq at:[root@wangyuelou Python-2.7.2]# vim /usr/bin/yum#!/usr/bin/python #修改此处为2.4的位置[root@~]# vim /usr/bin/yum#!/usr/bin/python2.4[root@~]# yumLoaded plugins: fastestmirrorYou need to give some commandusage: yum [options] COMMANDList of Commands:check-update Check for available package updatesclean Remove cached datadeplist List a package's dependenciesdowngrade downgrade a packageerase Remove a package or packages from your systemgroupinfo Display details about a package groupgroupinstall Install the packages in a group on your systemgrouplist List available package groupsgroupremove Remove the packages in a group from your systemhelp Display a helpful usage messageinfo Display details about a package or group of packagesinstall Install a package or packages on your systemlist List a package or groups of packageslocalinstall Install a local RPMyum 又可以使用了)# Process the JSON string.results = simplejson.load(response)# now have some fun with the results...
实际应用中可能需要抓取google的很多网页,所以还需要使用多线程来分担抓取任务。使用google web search api的参考详细介绍,请看此处(这里介绍了Standard URL Arguments)。另外要特别注意,url中参数rsz必须是8(包括8)以下的值,若大于8,会报错的!
(3)代码实现
代码实现还存在问题,但是能够运行,鲁棒性差,还需要进行改进,希望各路大神指出错误(初学Python),不胜感激。
python代码如下:
#-*-coding:utf-8-*-
import urllib2,urllib
import simplejson
import os, time,threading
import common, html_filter
#input the keywords
keywords = raw_input('Enter the keywords: ')
#define rnum_perpage, pages
rnum_perpage=8
pages=8
#定义线程函数
def thread_scratch(url, rnum_perpage, page):
url_set = []
try:
request = urllib2.Request(url, None, {'Referer': 'http://www.sina.com'})
response = urllib2.urlopen(request)
# Process the JSON string.
results = simplejson.load(response)
info = results['responseData']['results']
except Exception,e:
print 'error occured'
print e
else:
for minfo in info:
url_set.append(minfo['url'])
print minfo['url']
#处理链接
i = 0
for u in url_set:
try:
request_url = urllib2.Request(u, None, {'Referer': 'http://www.sina.com'})
request_url.add_header(
'User-agent',
'CSC'
)
response_data = urllib2.urlopen(request_url).read()
#过滤文件
#content_data = html_filter.filter_tags(response_data)
#写入文件
filenum = i+page
filename = dir_name+'/related_html_'+str(filenum)
print ' write start: related_html_'+str(filenum)
f = open(filename, 'w+', -1)
f.write(response_data)
#print content_data
f.close()
print ' write down: related_html_'+str(filenum)
except Exception, e:
print 'error occured 2'
print e
i = i+1
return
#创建文件夹
dir_name = 'related_html_'+urllib.quote(keywords)
if os.path.exists(dir_name):
print 'exists file'
common.delete_dir_or_file(dir_name)
os.makedirs(dir_name)
#抓取网页
print 'start to scratch web pages:'
for x in range(pages):
print "page:%s"%(x+1)
page = x * rnum_perpage
url = ('https://ajax.googleapis.com/ajax/services/search/web'
'?v=1.0&q=%s&rsz=%s&start=%s') % (urllib.quote(keywords), rnum_perpage,page)
print url
t = threading.Thread(target=thread_scratch, args=(url,rnum_perpage, page))
t.start()
#主线程等待子线程抓取完
main_thread = threading.currentThread()
for t in threading.enumerate():
if t is main_thread:
continue
t.join()
# 来自www.idcnote.com
注:关于python2.4升级2.7.2入门实例的内容就先介绍到这里,更多相关文章的可以留意
代码注释