2

我已经在这里发帖一段时间了,试图找出我看不见的 python 问题的原因。在本地,我的脚本运行良好,但是当我上传它时,它只执行了一半。

我的 python 脚本生成一个 html 文件。我已经对 python 脚本进行了 cron-jobbed,以便我的文件每隔几分钟更新一次。但是,它只是创建前几行代码并停止。

我相信原因是(经过一些调查)我的服务器正在运行 Python 2.4 而我正在运行 2.7。但是,我不确定如何将我的脚本升级(降级?)到 2.4。我认为这只是我存在的祸根的一行代码。

以下是相关代码:

phone.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'

感谢所有的帮助。

泰勒

4

2 回答 2

1

好的,所以 urllib2 也有同样的问题......更仔细地查看错误报告,我发现它正在尝试处理错误。urllib.py 的第 322 行尝试使用 wget 或类似方法,以确保您可以从服务器访问您尝试访问的 URL。如果可以的话,将 urllib 复制到某个地方,您可以在 pythonpath 上对其进行编辑并添加一些调试信息以找出它认为有错误的原因。由于我似乎无法在 2.4 上重现该问题,并且 2.4 早已停止服务,因此您需要跟踪发生的情况以修复它。我的猜测是fp在第 322 行上应该设置为 self.fp,但我不知道它是否默认为 None 并且没有设置,或者它是否传入了 None。另外,python2.4 的次要版本是什么你在跑步吗?我有 2.4.3,如果你愿意,我可以让我安装的 urllib.py 可用,你可以运行 diff 看看它们之间是否有区别。

于 2012-12-04T00:52:21.830 回答
0

我改用 Pythonwhere.com,没问题!

于 2012-12-04T02:46:16.840 回答