我一直在尝试在 PythonAnywhere 上尝试让一些 python 在网络服务器上工作。我最初是从 Arvixe 切换过来的,因为它们运行的是 2.4,而且 PythonAnywhere 的名字太吸引人了。
我的应用程序包含两个文件:phones.py 和 phonesearch.py。他们应该一起在craigslist上搜索电话价格。
我在 2.7 中进行了本地测试,它运行得很好,生成了一个带有表格和所有价格的 html 页面 (celly.html)。当我上传它时,它会很好地生成 html,但拒绝向我的价格表 ([intprices]) 添加任何内容。
我的怀疑:(a)因为它在本地运行良好,PythonAnywhere 不允许它与 craigslist 通信;或 (b) 因为我像穴居人一样做这件事,而不是使用微框架,PythonAnywhere 拒绝了我;或者 (c) 我对自己的错误视而不见,我错过了一些明显的东西。
我的 python 脚本位于 /home/tseymour/mysite 中,html 是在相同的/mysite/static/celly.html 中生成的。该文件在http://tseymour.pythonanywhere.com/static/celly.html提供
您会注意到我所有的单元格都填充了“N/A”,这意味着它在 SearchPhone.py 中的 try:" 中引发了 IndexError。这意味着我的列表正在被填充!
但这是为什么呢?!我相信这是因为我是 PythonAnywhere n00b。
请指教。
搜索电话.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)
电话.py
from SearchPhone import SearchPhone
phones = ["Iphone 4", "Iphone 5","Galaxy s3", "Galaxy s2", "LG Lucid", "LG Esteem", "HTC One S", "Droid 4",
"Droid RAZR MAXX", "HTC EVO", "Galaxy Nexus", "LG Optimus 2", "LG Ignite",
"Galaxy Note", "HTC Amaze", "HTC Rezound", "HTC Vivid", "HTC Rhyme", "Motorola Photon",
"Motorola Milestone", "myTouch slide", "HTC Status", "Droid 3", "HTC Evo 3d", "HTC Wildfire",
"LG Optimus 3d", "HTC ThunderBolt", "Incredible 2", "Kyocera Echo", "Galaxy S 4g",
"HTC Inspire", "LG Optimus 2x", "Samsung Gem", "HTC Evo Shift", "Nexus S", "LG Axis", "Droid 2",
"G2", "Droid x", "Droid Incredible"
]
f = open('/home/tseymour/mysite/static/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 "SEarchphone0"
y = SearchPhone(x)
print "SEarchphone"
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()
另外,我确实上传了beautifulsoup,以防万一