Tyl*_*our 3 python pythonanywhere
我一直在试验PythonAnywhere试图让一些python在Web服务器上工作.我最初从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",这意味着它在try:"在SearchPhone.py中引发了一个IndexError.这意味着我的列表正在被填充!
但为什么会这样?!我相信这是因为我是PythonAnywhere n00b.
请指教.
SearchPhone.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)
Run Code Online (Sandbox Code Playgroud)
phones.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()
Run Code Online (Sandbox Code Playgroud)
另外,我确实上传了beautifulsoup
归档时间: |
|
查看次数: |
1681 次 |
最近记录: |