1. 怎么用python生成列表[h1,h2,h3,h4],一直到h100
["h%s"%(i+1) for i in range(100)]
2. python中怎么给编好的程序加一个含有按钮的界面,按下按钮开始执行。再来一个结束执行的按钮
System.Diagnostics.Process.Start(@"你的Phython程序路径");
3. 如何创建和使用Python CGI脚本
在这个教程里,我们假设Apache web服务器已经安装好,并已运行。这篇教程使用的Apache web服务器(版本2.2.15,用于CentOS发行版6.5)运行在本地主机(127.0.0.1),并且监听80端口,如下面的Apache指令指定一样:
ServerName127.0.0.1:80
Listen80
下面举例中的HTML文件存放在web服务器上的/var/www/html目录下,并通过DocumentRoot指令指定(指定网页文件所在目录):
DocumentRoot"/var/www/html"
现在尝试请求URL:http://localhost/page1.html
这将返回web服务器中下面文件的内容:
/var/www/html/page1.html
为了启用CGI脚本,我们必须指定CGI脚本在web服务器上的位置,需要用到ScriptAlias指令:
ScriptAlias/cgi-bin/"/var/www/cgi-bin/"
以上指令表明CGI脚本保存在web服务器的/var/www/cgi-bin目录,请求URL里包含/cgi-bin/的将会搜索这个目录下的CGI脚本。
我们必须还要明确CGI脚本在/var/www/cgi-bin目录下有执行权限,还要指定CGI脚本的文件扩展名。使用下面的指令:
<Directory"/var/www/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
下面访问URL:http://localhost/cgi-bin/myscript-1.py
这将会调用web服务器中下面所示脚本:
/var/www/cgi-bin/myscript-1.py
创建一个CGI脚本
在创建一个Python CGI脚本之前,你需要确认你已经安装了Python(这通常是默认安装的,但是安装版本可能会有所不同)。本篇教程使用的脚本是使用Python版本2.6.6编写的。你可以通过下面任意一命令(-V和--version参数将显示所安装Python的版本号)检查Python的版本。
$ python -V
$ python --version
如果你的Python CGI脚本要用来处理用户输入的数据(从一个web输入表单),那么你将需要导入Python cgi模块。这个模块可以处理用户通过web输入表单输入的数据。你可以在你的脚本中通过下面的语句导入该脚本:
import cgi
你也必须修改Python CGI脚本的执行权限,以防止web服务器不能调用。可以通过下面的命令增加执行权限:
# chmod o+x myscript-1.py
Python CGI例子
涉及到Python CGI脚本的两个方案将会在下面讲述:
使用Python脚本创建一个网页
读取并显示用户输入的数据,并且在网页上显示结果
注意:Python cgi模块在方案2中是必需的,因为这涉及到用户从web表单输入数据。
例子1 :使用Python脚本创建一个网页
对于这个方案,我们将通过创建包含一个单一提交按钮的网页/var/www/html/page1.html开始。
<html>
<h1>Test Page 1</h1>
<formname="input"action="/cgi-bin/myscript-1.py"method="get">
<inputtype="submit"value="Submit">
</form>
</html>
当"提交"按钮被点击,/var/www/cgi-bin/myscript-1.py脚本将被调用(通过action参数指定)。通过设置方法参数为"get"来指定一个"GET"请求,服务器将会返回指定的网页。/var/www/html/page1.html在浏览器中的显示情况如下:
/var/www/cgi-bin/myscript-1.py的内容如下:
#!/usr/bin/python
print"Content-Type: text/html"
print""
print"<html>"
print"<h2>CGI Script Output</h2>"
print"<p>This page was generated by a Python CGI script.</p>"
print"</html>"
第一行声明表示这是使用 /usr/bin/python命令运行的Python脚本。"Content-Type: text/html"打印语句是必需的,这是为了让web服务器知道接受自CGI脚本的输出类型。其余的语句用来输出HTML格式的其余网页内容。
当"Submit"按钮点击,下面的网页将返回:
这个例子的要点是你可以决定哪些信息可以被CGI脚本返回。这可能包括日志文件的内容,当前登陆用户的列表,或者今天的日期。在你处理时拥有所有python库的可能性是无穷无尽的。
例子2:读取并显示用户输入的数据,并将结果显示在网页上
对于这个方案,我们将通过创建一个含有三个输入域和一个提交按钮的网页/var/www/html/page2.html开始。
<html>
<h1>Test Page 2</h1>
<formname="input"action="/cgi-bin/myscript-2.py"method="get">
First Name: <inputtype="text"name="firstName"><br>
Last Name: <inputtype="text"name="lastName"><br>
Position: <inputtype="text"name="position"><br>
<inputtype="submit"value="Submit">
</form>
</html>
当"Submit"按钮点击,/var/www/cgi-bin/myscript-2.py脚本将被执行(通过action参数指定)。/var/www//html/page2.html显示在web浏览器中的图片如下所示(注意,三个输入域已经被填写好了):
/var/www/cgi-bin/myscript-2.py的内容如下:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print"Content-Type: text/html"
print""
print"<html>"
print"<h2>CGI Script Output</h2>"
print"<p>"
print"The user entered data are:<br>"
print"<b>First Name:</b> "+ form["firstName"].value +"<br>"
print"<b>Last Name:</b> "+ form["lastName"].value +"<br>"
print"<b>Position:</b> "+ form["position"].value +"<br>"
print"</p>"
print"</html>"
正如前面提到,import cgi语句用来确保能够处理用户通过web输入表单输入的数据。web输入表单被封装在一个表单对象中,叫做cgi.FieldStorage对象。一旦开始输出,"Content-Type: text/html"是必需的,因为web服务器需要知道接受自CGI脚本的输出格式。用户输入的数据在包含form["firstName"].value,form["lastName"].value,和 form["position"].value的语句中可以得到。那些中括号中的名称和/var/www/html/page2.html文本输入域中定义的名称参数一致。
当网页上的"Submit"按钮被点击,下面的网页将被返回。
这个例子的要点就是你可以很容易地读取并显示用户在web表单上输入的数据。除了以字符串的方式处理数据,你也可以用Python将用户输入的数据转化为可用于数值计算的数字。
4. Python 能不能到函数里才执行实参
通过变通的方式是可以实现的,比如把实参封装成函数。
Number=lambda:response.xpath('/html/body').re('(?<="proct_no"value=").*?(?=")')[0]
在函数中通过Number()调用
如果解决了您的问题请采纳!
如果未解决请继续追问
5. python正则匹配不到想要的数据,感觉明明是对的啊
items = re.findall(r'',pagecode,re.S)
注意在后面要加上re.S
因为是多行匹配,下面写了一个只能抓一页信息的简陋版- -
-------------------------------------------------------------------------------------------
#coding:utf-8
importurllib2,re
classdown(object):
def__init__(self):
self.user_agent='Mozilla/4.0(compatible;MSIE5.5;WindowsNT)'
self.headers={'User-Agent':self.user_agent}
defgetPage(self):
try:
url='http://hdwan.net'
request=urllib2.Request(url,headers=self.headers)
response=urllib2.urlopen(request)
pageCode=response.read().decode('utf-8')
returnpageCode
excepturllib2.URLError,e:
ifhasattr(e,'reason'):
printu'连接失败...',e.reason
returnNone
defgetCont(self):
pageCode=self.getPage()
ifnotpageCode:
printu'页面加载失败...'
returnNone
items=re.findall('''<h2><ahref="(.*?)"rel="bookmark"target="_blank"title="(.*?)">.*?
</a></h2>''',pageCode,re.S)
foriinitems:
printu'电影名:%s 链接:%s '%(i[1],i[0])
if__name__=='__main__':
spider=down()
spider.getCont()
初学者一起加油。。。。
6. 如何用python爬取豆瓣读书的数据
这两天爬了豆瓣读书的十万条左右的书目信息,用时将近一天,现在趁着这个空闲把代码总结一下,还是菜鸟,都是用的最简单最笨的方法,还请路过的大神不吝赐教。
第一步,先看一下我们需要的库:
import requests #用来请求网页
from bs4 import BeautifulSoup #解析网页
import time #设置延时时间,防止爬取过于频繁被封IP号
import re #正则表达式库
import pymysql #由于爬取的数据太多,我们要把他存入MySQL数据库中,这个库用于连接数据库
import random #这个库里用到了产生随机数的randint函数,和上面的time搭配,使爬取间隔时间随机
这个是豆瓣的网址:x-sorttags-all
我们要从这里获取所有分类的标签链接,进一步去爬取里面的信息,代码先贴上来:
import requests
from bs4 import BeautifulSoup #导入库
url="httom/tag/?icn=index-nav"
wb_data=requests.get(url) #请求网址
soup=BeautifulSoup(wb_data.text,"lxml") #解析网页信息
tags=soup.select("#content > div > div.article > div > div > table > tbody > tr > td > a")
#根据CSS路径查找标签信息,CSS路径获取方法,右键-检查- selector,tags返回的是一个列表
for tag in tags:
tag=tag.get_text() #将列表中的每一个标签信息提取出来
helf="hom/tag/"
#观察一下豆瓣的网址,基本都是这部分加上标签信息,所以我们要组装网址,用于爬取标签详情页
url=helf+str(tag)
print(url) #网址组装完毕,输出
以上我们便爬取了所有标签下的网址,我们将这个文件命名为channel,并在channel中创建一个channel字符串,放上我们所有爬取的网址信息,等下爬取详情页的时候直接从这里提取链接就好了,如下:
channel='''
tag/程序
'''
现在,我们开始第二个程序。
QQ图片20160915233329.png
标签页下每一个图片的信息基本都是这样的,我们可以直接从这里提取到标题,作者,出版社,出版时间,价格,评价人数,以及评分等信息(有些外国作品还会有译者信息),提取方法与提取标签类似,也是根据CSS路径提取。
我们先用一个网址来实验爬取:
url="htt/tag/科技"
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text.encode("utf-8"), "lxml")
tag=url.split("?")[0].split("/")[-1] #从链接里面提取标签信息,方便存储
detils=soup.select("#subject_list > ul > li > div.info > div.pub") #抓取作者,出版社信息,稍后我们用spite()函数再将他们分离出来
scors=soup.select("#subject_list > ul > li > div.info > div.star.clearfix > span.rating_nums") #抓取评分信息
persons=soup.select("#subject_list > ul > li > div.info > div.star.clearfix > span.pl") #评价人数
titles=soup.select("#subject_list > ul > li > div.info > h2 > a") #书名
#以上抓取的都是我们需要的html语言标签信息,我们还需要将他们一一分离出来
for detil,scor,person,title in zip(detils,scors,persons,titles):
#用一个zip()函数实现一次遍历
#因为一些标签中有译者信息,一些标签中没有,为避免错误,所以我们要用一个try来把他们分开执行
try:
author=detil.get_text().split("/",4)[0].split()[0] #这是含有译者信息的提取办法,根据“/” 把标签分为五部分,然后依次提取出来
yizhe= detil.get_text().split("/", 4)[1]
publish=detil.get_text().split("/", 4)[2]
time=detil.get_text().split("/", 4)[3].split()[0].split("-")[0] #时间我们只提取了出版年份
price=ceshi_priceone(detil) #因为价格的单位不统一,我们用一个函数把他们换算为“元”
scoe=scor.get_text() if True else "" #有些书目是没有评分的,为避免错误,我们把没有评分的信息设置为空
person=ceshi_person(person) #有些书目的评价人数显示少于十人,爬取过程中会出现错误,用一个函数来处理
title=title.get_text().split()[0]
#当没有译者信息时,会显示IndexError,我们分开处理
except IndexError:
try:
author=detil.get_text().split("/", 3)[0].split()[0]
yizhe="" #将detil信息划分为4部分提取,译者信息直接设置为空,其他与上面一样
publish=detil.get_text().split("/", 3)[1]
time=detil.get_text().split("/", 3)[2].split()[0].split("-")[0]
price=ceshi_pricetwo(detil)
scoe=scor.get_text() if True else ""
person=ceshi_person(person)
title=title.get_text().split()[0]
except (IndexError,TypeError):
continue
#出现其他错误信息,忽略,继续执行(有些书目信息下会没有出版社或者出版年份,但是数量很少,不影响我们大规模爬取,所以直接忽略)
except TypeError:
continue
#提取评价人数的函数,如果评价人数少于十人,按十人处理
def ceshi_person(person):
try:
person = int(person.get_text().split()[0][1:len(person.get_text().split()[0]) - 4])
except ValueError:
person = int(10)
return person
#分情况提取价格的函数,用正则表达式找到含有特殊字符的信息,并换算为“元”
def ceshi_priceone(price):
price = detil.get_text().split("/", 4)[4].split()
if re.match("USD", price[0]):
price = float(price[1]) * 6
elif re.match("CNY", price[0]):
price = price[1]
elif re.match("A$", price[0]):
price = float(price[1:len(price)]) * 6
else:
price = price[0]
return price
def ceshi_pricetwo(price):
price = detil.get_text().split("/", 3)[3].split()
if re.match("USD", price[0]):
price = float(price[1]) * 6
elif re.match("CNY", price[0]):
price = price[1]
elif re.match("A$", price[0]):
price = float(price[1:len(price)]) * 6
else:
price = price[0]
return price
实验成功后,我们就可以爬取数据并导入到数据库中了,以下为全部源码,特殊情况会用注释一一说明。
import requests
from bs4 import BeautifulSoup
import time
import re
import pymysql
from channel import channel #这是我们第一个程序爬取的链接信息
import random
def ceshi_person(person):
try:
person = int(person.get_text().split()[0][1:len(person.get_text().split()[0]) - 4])
except ValueError:
person = int(10)
return person
def ceshi_priceone(price):
price = detil.get_text().split("/", 4)[4].split()
if re.match("USD", price[0]):
price = float(price[1]) * 6
elif re.match("CNY", price[0]):
price = price[1]
elif re.match("A$", price[0]):
price = float(price[1:len(price)]) * 6
else:
price = price[0]
return price
def ceshi_pricetwo(price):
price = detil.get_text().split("/", 3)[3].split()
if re.match("USD", price[0]):
price = float(price[1]) * 6
elif re.match("CNY", price[0]):
price = price[1]
elif re.match("A$", price[0]):
price = float(price[1:len(price)]) * 6
else:
price = price[0]
return price
#这是上面的那个测试函数,我们把它放在主函数中
def mains(url):
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text.encode("utf-8"), "lxml")
tag=url.split("?")[0].split("/")[-1]
detils=soup.select("#subject_list > ul > li > div.info > div.pub")
scors=soup.select("#subject_list > ul > li > div.info > div.star.clearfix > span.rating_nums")
persons=soup.select("#subject_list > ul > li > div.info > div.star.clearfix > span.pl")
titles=soup.select("#subject_list > ul > li > div.info > h2 > a")
for detil,scor,person,title in zip(detils,scors,persons,titles):
l = [] #建一个列表,用于存放数据
try:
author=detil.get_text().split("/",4)[0].split()[0]
yizhe= detil.get_text().split("/", 4)[1]
publish=detil.get_text().split("/", 4)[2]
time=detil.get_text().split("/", 4)[3].split()[0].split("-")[0]
price=ceshi_priceone(detil)
scoe=scor.get_text() if True else ""
person=ceshi_person(person)
title=title.get_text().split()[0]
except IndexError:
try:
author=detil.get_text().split("/", 3)[0].split()[0]
yizhe=""
publish=detil.get_text().split("/", 3)[1]
time=detil.get_text().split("/", 3)[2].split()[0].split("-")[0]
price=ceshi_pricetwo(detil)
scoe=scor.get_text() if True else ""
person=ceshi_person(person)
title=title.get_text().split()[0]
except (IndexError,TypeError):
continue
except TypeError:
continue
l.append([title,scoe,author,price,time,publish,person,yizhe,tag])
#将爬取的数据依次填入列表中
sql="INSERT INTO allbooks values(%s,%s,%s,%s,%s,%s,%s,%s,%s)" #这是一条sql插入语句
cur.executemany(sql,l) #执行sql语句,并用executemary()函数批量插入数据库中
conn.commit()
#主函数到此结束
# 将Python连接到MySQL中的python数据库中
conn = pymysql.connect( user="root",password="123123",database="python",charset='utf8')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS allbooks') #如果数据库中有allbooks的数据库则删除
sql = """CREATE TABLE allbooks(
title CHAR(255) NOT NULL,
scor CHAR(255),
author CHAR(255),
price CHAR(255),
time CHAR(255),
publish CHAR(255),
person CHAR(255),
yizhe CHAR(255),
tag CHAR(255)
)"""
cur.execute(sql) #执行sql语句,新建一个allbooks的数据库
start = time.clock() #设置一个时钟,这样我们就能知道我们爬取了多长时间了
for urls in channel.split():
urlss=[urls+"?start={}&type=T".format(str(i)) for i in range(0,980,20)] #从channel中提取url信息,并组装成每一页的链接
for url in urlss:
mains(url) #执行主函数,开始爬取
print(url) #输出要爬取的链接,这样我们就能知道爬到哪了,发生错误也好处理
time.sleep(int(format(random.randint(0,9)))) #设置一个随机数时间,每爬一个网页可以随机的停一段时间,防止IP被封
end = time.clock()
print('Time Usage:', end - start) #爬取结束,输出爬取时间
count = cur.execute('select * from allbooks')
print('has %s record' % count) #输出爬取的总数目条数
# 释放数据连接
if cur:
cur.close()
if conn:
conn.close()
这样,一个程序就算完成了,豆瓣的书目信息就一条条地写进了我们的数据库中,当然,在爬取的过程中,也遇到了很多问题,比如标题返回的信息拆分后中会有空格,写入数据库中会出现错误,所以只截取了标题的第一部分,因而导致数据库中的一些书名不完整,过往的大神如果有什么办法,还请指教一二。
等待爬取的过程是漫长而又欣喜的,看着电脑上一条条信息被刷出来,成就感就不知不觉涌上心头;然而如果你吃饭时它在爬,你上厕所时它在爬,你都已经爬了个山回来了它还在爬时,便会有点崩溃了,担心电脑随时都会坏掉(还是穷学生换不起啊啊啊啊~)
所以,还是要好好学学设置断点,多线程,以及正则,路漫漫其修远兮,吾将上下而求索~共勉~
7. 如何用 Python 爬取社交网络
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Administrator
# @Date: 2015-10-31 15:45:27
# @Last Modified by: Administrator
# @Last Modified time: 2015-11-23 16:57:31
import requests
import sys
import json
import re
reload(sys)
sys.setdefaultencoding('utf-8')
#获取到匹配字符的字符串
def find(pattern,test):
finder = re.search(pattern, test)
start = finder.start()
end = finder.end()
return test[start:end-1]
cookies = {
'_ga':'GA1.2.10sdfsdfsdf', '_za':'8d570b05-b0b1-4c96-a441-faddff34',
'q_c1':'23ddd234234',
'_xsrf':'234id':'"ZTE3NWY2ZTsdfsdfsdfWM2YzYxZmE=|1446435757|"',
'z_c0':'"=|14464e234767|"',
'__utmt':'1', '__utma':'51854390.109883802f8.1417518721.1447917637.144c7922009.4',
'__utmb':'518542340.4.10.1447922009', '__utmc':'51123390', '__utmz':'5185435454sdf06.1.1.utmcsr=hu.com|utmcgcn=(referral)|utmcmd=referral|utmcct=/',
'__utmv':'51854340.1d200-1|2=registration_date=2028=1^3=entry_date=201330318=1'}
headers = {'user-agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36',
'referer':'http://www.hu.com/question/following',
'host':'www.hu.com','Origin':'http://www.hu.com',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Connection':'keep-alive','X-Requested-With':'XMLHttpRequest','Content-Length':'81',
'Accept-Encoding':'gzip,deflate','Accept-Language':'zh-CN,zh;q=0.8','Connection':'keep-alive'
}
#多次访问之后,其实一加载时加载20个问题,具体参数传输就是offset,以20递增
dicc = {"offset":60}
n=20
b=0
# 与爬取图片相同的是,往下拉的时候也会发送http请求返回json数据,但是不同的是,像模拟登录首页不同的是除了
# 发送form表单的那些东西后,知乎是拒绝了我的请求了,刚开始以为是headers上的拦截,往headers添加浏览器
# 访问是的headers那些信息添加上,发现还是拒绝访问。
#想了一下,应该是cookie原因。这个加载的请求和模拟登录首页不同
#所以补上其他的cookies信息,再次请求,请求成功。
for x in xrange(20,460,20):
n = n+20
b = b+20
dicc['offset'] = x
formdata = {'method':'next','params':'{"offset":20}','_xsrf':''}
#传输需要json串,和python的字典是有区别的,需要转换
formdata['params'] = json.mps(dicc)
# print json.mps(dicc)
# print dicc
circle = requests.post("http://www.hu.com/node/ProfileFollowedQuestionsV2",
cookies=cookies,data=formdata,headers=headers)
#response内容 其实爬过一次之后就大同小异了。 都是
#问题返回的json串格式
# {"r":0,
# "msg": ["<div class=\"zm-profile-section-item zg-clear\">\n
# <span class=\"zm-profile-vote-count\">\n<div class=\"zm-profile-vote-num\">205K<\/div>\n
# <div class=\"zm-profile-vote-type\">\u6d4f\u89c8<\/div>\n
# <\/span>\n<div class=\"zm-profile-section-main\">\n
# <h2 class=\"zm-profile-question\">\n
# <a class=\"question_link\" target=\"_blank\" href=\"\/question\/21719532\">
# \u4ec0\u4e48\u4fc3\u4f7f\u4f60\u8d70\u4e0a\u72ec\u7acb\u5f00\u53d1\u8005\u4e4b\u8def\uff1f<\/a>\n
# <\/h2>\n<div class=\"meta zg-gray\">\n<a data-follow=\"q:link\" class=\"follow-link zg-unfollow meta-item\"
# href=\"javascript:;\" id=\"sfb-868760\">
# <i class=\"z-icon-follow\"><\/i>\u53d6\u6d88\u5173\u6ce8<\/a>\n<span class=\"zg-bull\">•<\/span>\n63 \u4e2a\u56de\u7b54\n<span class=\"zg-bull\">•<\/span>\n3589 \u4eba\u5173\u6ce8\n<\/div>\n<\/div>\n<\/div>",
# "<div class=\"zm-profile-section-item zg-clear\">\n
# <span class=\"zm-profile-vote-count\">\n
# <div class=\"zm-profile-vote-num\">157K<\/div>\n
# <div class=\"zm-profile-vote-type\">\u6d4f\u89c8<\/div>\n
# <\/span>\n<div class=\"zm-profile-section-main\">\n
# <h2 class=\"zm-profile-question\">\n
# <a class=\"question_link\" target=\"_blank\" href=\"\/question\/31764065\">
# \u672c\u79d1\u6e23\u6821\u7684\u5b66\u751f\u5982\u4f55\u8fdb\u5165\u7f8e\u5e1d\u725b\u6821\u8bfbPhD\uff1f<\/a>\n
# <\/h2>\n<div class=\"meta zg-gray\">\n
# <a data-follow=\"q:link\" class=\"follow-link zg-unfollow meta-item\" href=\"javascript:;\" id=\"sfb-4904877\">
# <i class=\"z-icon-follow\"><\/i>\u53d6\u6d88\u5173\u6ce8<\/a>\n<span class=\"zg-bull\">•
# <\/span>\n112 \u4e2a\u56de\u7b54\n<span class=\"zg-bull\">•<\/span>\n1582 \u4eba\u5173\u6ce8\n
# <\/div>\n<\/div>\n<\/div>"]}
# print circle.content
#同样json串需要自己 转换成字典后使用
jsondict = json.loads(circle.text)
msgstr = jsondict['msg']
# print len(msgstr)
#根据自己所需要的提取信息规则写出正则表达式
pattern = 'question\/.*?/a>'
try:
for y in xrange(0,20):
wholequestion = find(pattern, msgstr[y])
pattern2 = '>.*?<'
finalquestion = find(pattern2, wholequestion).replace('>','')
print str(b+y)+" "+finalquestion
#当问题已经访问完后再传参数 抛出异常 此时退出循环
except Exception, e:
print "全部%s个问题" %(b+y)
break
8. Python 制作网页打不开 直接跳到打开或者保存文件
需要对Lighttpd进行配置,指明py文件是作为cgi程序运行的。
修改配置文件:/etc/lighttpd/lighttpd.conf
在以下小节内添加python内容:
server.moles = ( "mod_cgi", )
cgi.assign = (
".py" => "/usr/bin/python"
)
9. python一个函数qh2(x,y,z),表示从x加到y,每次增加z怎么处理
包括y的话:
def qh2(x, y, z):
result = 0
for i in range(x, y+1, z):
result += i
return result