Python2.7和2.4之间的差异会产生一个错误
我在这里发布了一段时间,试图找到我的隐形python问题的原因。在本地,我的脚本运行良好,但当我上传它时,它只执行了一半 我的python脚本生成一个html文件。我让cron处理python脚本,以便每隔几分钟更新一次我的文件。然而,它只是创建了前几行代码并停止了 我相信原因是(经过一些调查)我的服务器运行的是Python2.4,而我运行的是2.7。然而,我不知道如何将我的脚本升级(降级?)到2.4。我认为这只是一行代码,是我存在的祸根 以下是相关代码: phones.py:调用另一个文件SearchPhone并将html生成到celly.html中Python2.7和2.4之间的差异会产生一个错误,python,python-2.7,python-2.4,Python,Python 2.7,Python 2.4,我在这里发布了一段时间,试图找到我的隐形python问题的原因。在本地,我的脚本运行良好,但当我上传它时,它只执行了一半 我的python脚本生成一个html文件。我让cron处理python脚本,以便每隔几分钟更新一次我的文件。然而,它只是创建了前几行代码并停止了 我相信原因是(经过一些调查)我的服务器运行的是Python2.4,而我运行的是2.7。然而,我不知道如何将我的脚本升级(降级?)到2.4。我认为这只是一行代码,是我存在的祸根 以下是相关代码: phones.py:调用另一个文件Se
from SearchPhone import SearchPhone
phones = ["iphone 4", "iphone 5", "iphone 3"]
f = open('celly.html','w')
f.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Celly Blue Book</title>
</head>
<body>
</body>
</html>
""")
#table
f.write('<table width="100%" border="1">')
for x in phones:
print "Pre-Searchphone DEBUG" ##THIS PRINTS!!
y = SearchPhone(x) ## <--Here is the culprit.
print "Post-SearchPhone DEBUG" ##THIS DOES NOT!!
f.write( "\t<tr>")
f.write( "\t\t<td>" + str(y[0]) + "</td>")
f.write( "\t\t<td>" + str(y[1]) + "</td>")
f.write( "\t\t<td>" + str(y[2]) + "</td>")
f.write( "\t\t<td>" + str(y[3]) + "</td>")
f.write( "\t\t<td>" + str(y[4]) + "</td>")
f.write( "\t</tr>")
f.write('</table>')
f.close()
谢谢大家的帮助
泰勒好的,urllib2也有同样的问题。。。仔细查看错误报告,我发现它正在尝试处理错误。urllib.py的第322行尝试使用wget或类似工具,以确保您可以从服务器访问您尝试访问的URL。如果可以的话,将urllib复制到pythonpath上的某个地方进行编辑,并添加一些调试信息以找出它认为存在错误的原因。由于我似乎无法重现2.4上的问题,并且2.4已长期停止使用,因此您需要跟踪正在发生的情况以修复它。我猜第322行的
fp
应该设置为self.fp,但我不知道它是否默认为None并且没有设置,或者是否传入了None。另外,您正在运行python2.4的哪个次要版本?我有2.4.3,如果你愿意,我可以从我的安装中使用urllib.py,你可以运行一个diff来看看它们之间是否有区别。我改到Pythonwhere.com,没有问题 如果你的部署平台停留在Python2.4上,为什么不在你的开发平台上安装相同的版本(既可以调试这个问题,也可以用于以后的开发)?
from BeautifulSoup import BeautifulSoup
import urllib
import re
def SearchPhone(phone):
y = "http://losangeles.craigslist.org/search/moa?query=" + phone + "+-%22buy%22+-%22fix%22+-%22unlock%22+-%22broken%22+-%22cracked%22+-%22parts%22&srchType=T&minAsk=&maxAsk="
site = urllib.urlopen(y)
html = site.read()
site.close()
soup = BeautifulSoup(html)
prices = soup.findAll("span", {"class":"itempp"})
prices = [str(j).strip('<span class="itempp"> $</span>') for j in prices]
for k in prices[:]:
if k == '': #left price blank
prices.remove(k)
elif int(k) <= 75: #less than $50: probably a service (or not true)
prices.remove(k)
elif int(k) >= 999: #probably not true
prices.remove(k)
#Find Average Price
intprices = []
newprices = prices[:]
total = 0
for k in newprices:
total += int(k)
intprices.append(int(k))
intprices = sorted(intprices)
try:
del intprices[0]
del intprices[-1]
avg = total/len(newprices)
low = intprices[0]
high = intprices[-1]
if len(intprices) % 2 == 1:
median = intprices[(len(intprices)+1)/2-1]
else:
lower = intprices[len(intprices)/2-1]
upper = intprices[len(intprices)/2]
median = (float(lower + upper)) / 2
namestr = str(phone)
medstr = "Median: $" + str(median)
avgstr = "Average: $" + str(avg)
lowstr = "Low: $" + str(intprices[0])
highstr = "High: $" + str(intprices[-1])
samplestr = "# of samples: " + str(len(intprices))
linestr = "-------------------------------"
except IndexError:
namestr = str(phone)
medstr = "N/A"
avgstr = "N/A"
lowstr = "N/A"
highstr = "N/A"
samplestr = "N/A"
linestr = "-------------------------------"
return (namestr, medstr, avgstr, lowstr, highstr, samplestr, linestr)
Pre-SearchPhone DEBUG
Traceback (most recent call last):
File "/home/tseymour/public_html/celly/phones.py", line 35, in ?
y = SearchPhone(x)
File "/home/tseymour/public_html/celly/SearchPhone.py", line 11, in SearchPhone
site = urllib.urlopen(y)
File "/usr/lib64/python2.4/urllib.py", line 82, in urlopen
return opener.open(url)
File "/usr/lib64/python2.4/urllib.py", line 190, in open
return getattr(self, name)(url)
File "/usr/lib64/python2.4/urllib.py", line 322, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "/usr/lib64/python2.4/urllib.py", line 339, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "/usr/lib64/python2.4/urllib.py", line 579, in http_error_default
return addinfourl(fp, headers, "http:" + url)
File "/usr/lib64/python2.4/urllib.py", line 883, in __init__
addbase.__init__(self, fp)
File "/usr/lib64/python2.4/urllib.py", line 830, in __init__
self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'