我一直在这里发帖一段时间试图找到我看不见的python问题的原因。在本地,我的脚本运行正常,但是当我上传它时,它只会执行一半。
我的python脚本生成一个html文件。我有cron-jobbed python脚本,以便我的文件每隔几分钟更新一次。但是,它只创建了前几行代码并停止了。
我相信(经过一些调查后)我的服务器运行Python 2.4并运行2.7的原因。但是,我不确定如何将我的脚本升级(降级?)到2.4。我认为这只是一行代码,这是我存在的祸根。
以下是相关代码:
phones.py:这会调用另一个文件SearchPhone并将html生成为celly.html
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()
SearchPhone.py:搜索手机并由phones.py
调用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'
感谢所有帮助人员。
泰勒
答案 0 :(得分:1)
好的,所以urllib2有同样的问题...仔细查看错误报告,我发现它正在尝试处理错误。 urllib.py的第322行尝试使用wget或类似方法确保您可以从服务器访问您尝试访问的URL。如果可以的话,将urllib复制到某个位置,你可以编辑它在python路径上,并添加一些调试信息,以找出它认为存在错误的原因。由于我似乎无法在2.4上重现问题,并且2.4很长时间没有服务,因此您需要追踪正在发生的事情才能解决问题。我的猜测是,第322行的fp
应该设置为self.fp,但我不知道它是否默认为None并且没有设置,或者是否传入了None。也你正在运行什么小版本的python2.4?我有2.4.3,如果你愿意的话,我可以让我的安装中的urllib.py可用,你可以运行差异来查看它们之间是否存在差异。
答案 1 :(得分:0)
我转到Pythonwhere.com,没有问题!