Skip to main content
 首页 » 编程设计

python之python 2.7 和 2.4 之间的差异产生错误

2025年05月04日14telwanggs

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

我的 python 脚本生成一个 html 文件。我已使用 cron-jobed 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' 

感谢大家的帮助。

泰勒

请您参考如下方法:

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