python在shell中运行正常,但在windows中经常报错

问题描述

python在shell中运行正常,但在windows中经常报错
我写了一个获取网页信息的文件,在shell中测试,运行情况良好,
但是直接双击打开py文件,则经常闪退。
以下是代码。这种情况我不是很了解,求帮助。

 # -*- coding: utf-8 -*-import urllib2import urllibimport reimport threadimport timeimport json#----------加载处理Steam市场--------------class Spider_Model:    def __init__(self):        self.enable = False        self.myUrl = ""http://steamcommunity.com/market/recent?country=CN&language=schinese&currency=1""        self.data = {""country"":""CN""language"":""schinese""currency"":""1""}    def getLastOn(selfpage):        #print(page)        #page = page.decode(""utf-8"")        #找出所有的class=""market_listing_item_name_link""的a        #找出所有的class=""market_listing_game_name""的span        #找出所有的class=""market_listing_price market_listing_price_with_fee""的span        Commodity_names = re.findall('<a class=""market_listing_item_name_link"".*?>(.*?)</a>'pagere.S)        Commodity_games = re.findall('<span.*?class=""market_listing_game_name"">(.*?)</span>'pagere.S)        Commodity_prices = re.findall('<span.*?class=""market_listing_price market_listing_price_with_fee"">(.*?)</span>'pagere.S)        #曾用来统计获取到的数据数量        #print len(Commodity_names)        #print len(Commodity_games)        #print len(Commodity_prices)        items = []        for i in range(010):            Commodity_prices[i] = Commodity_prices[i].replace(""$""$"")            #print[Commodity_prices[i]]            item = [Commodity_names[i]Commodity_games[i]Commodity_prices[i]]            items.append(item)            print items[i][0]+""n""+items[i][1]+items[i][2]+""n""        return items    #访问地址后返回的是Json文件    def request_ajax_data(self):        req = urllib2.Request(self.myUrl)        #伪装成浏览器访问        req.add_header('Content-Type' 'application/x-www-form-urlencoded; charset=UTF-8')        req.add_header('X-Requested-With''XMLHttpRequest')        req.add_header('User-Agent''Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML like Gecko) Chrome/27.0.1453.116')        params = urllib.urlencode(self.data)        #print(params)        response = urllib2.urlopen(req params)        jsonText = response.read()        #使原编码改为utf-8编码(中文太恶心了)        unicodePage = jsonText.decode(""utf-8"")        return unicodePage    #解析Json文件    def parseJson(selfjsonText):        j = json.loads(jsonText)        page = j[""results_html""]        #print(page)        return page    def start(self):        self.enable = True        while self.enable:            self.getLastOn(self.parseJson(self.request_ajax_data()))            print('========================^_^============================')            myInput = raw_input()            if myInput == 'q':                self.enable = False                break#------------程序入口处---------------print u'''---------------------------------------    程序:steam市场语言:Pythom 2.7版本:1.0作者:Cy操作:输入‘q’退出,按下回车获取最新上架资讯功能:获取steam市场上最新上架的信息---------------------------------------'''print u'~~~~'print u'请按下回车浏览即时steam最新上架:'  myInput = raw_input(' ')myModel = Spider_Model()#print(myModel.GetLastOn(myUrl))#myModel.request_ajax_data(myUrl)#print(myModel.request_ajax_data())#f = open('steam.json''w+')#f.write(myModel.request_ajax_data())#f.close()#tt = myModel.request_ajax_data()#page = myModel.parseJson(tt)#myModel.getLastOn(page)myModel.start()print u'谢谢使用!'#market.js/line:948&992

解决方案

是不是跟你的运行时路径有关系,shell中运行是py文件目录为当前目录,双击时,应该是python.exe的目录为当前目录

时间: 2024-08-03 13:05:30

python在shell中运行正常,但在windows中经常报错的相关文章

eclipse-安卓在Eclipse中运行项目,为什么虚拟机中不会直接运行项目?

问题描述 安卓在Eclipse中运行项目,为什么虚拟机中不会直接运行项目? 解决方案 贴出debug再百度一下就知道了 解决方案二: Eclipse不可不知的用法之四:配置服务器与项目的新建运行

web项目在eclipse中运行正常 部署到tomcat中运行报spring context错误

问题描述 web项目在eclipse中运行正常 部署到tomcat中运行报spring context错误 20C 解决方案 quatrz配置有问题参考:http://blog.csdn.net/kingzuo/article/details/12572881http://www.cnblogs.com/kay/archive/2007/11/02/947372.html

visual studio-SQL语句在VS中运行错误,在数据库中执行正确

问题描述 SQL语句在VS中运行错误,在数据库中执行正确 select (case when ub.ishandle is NUll then '未处理' when ub.ishandle='' then '未处理' else ishandle end)as 'ishandle',u.fsid,u.BuMenId,y.Chusheng,y.lunar,u.Realname,b.Name as bname from qp_hr_Yuangong y, qp_oa_Bumen b,qp_oa_use

unity2.0中在VS2010中运行正常,在IIS中无法注入

问题描述 unity2.0中在VS2010中运行正常,在IIS中无法注入Page页面通过public属性的方式注入servicepublicpartialclassLogin:System.Web.UI.Page{publicIUserServiceservice{set;get;}}注入是通过xml文件配置的在VS2010环境一切正常,发布到IIS7中sevice就为null了在protectedvoidPage_Load(objectsender,EventArgse){if(!IsPost

java spring-maven 项目中修改项目的包名后重新启动报错

问题描述 maven 项目中修改项目的包名后重新启动报错 原来项目运行时正常的,后来把com.fh.controller.information.picture.PictureController 和com.fh.service.information.picture.PictureService修改为com.fh.controller.information.PictureController 和com.fh.service.information.PictureService后出错 启动项目

javamail truts-做后台发送邮件,用main测试好试,单合到项目中Action里就没用,也没报错

问题描述 做后台发送邮件,用main测试好试,单合到项目中Action里就没用,也没报错 import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.interne

HibernateSessionFactory类运行到configuration.buildSessionFactory()这句话不运行下了,并且也没有报错

问题描述 HibernateSessionFactory类运行到configuration.buildSessionFactory()这句话不运行下了,并且也没有报错...hibernate.cfg.xml<hibernate-configuration><session-factory><propertyname="connection.url">jdbc:mysql://localhost:3306/hibernatetest</prope

Windows 安装补丁报错 0xc8000247 Windows update 当前无法检查更新,因为未运行服务。您可能需要重新启动计算机。

Windows 安装补丁报错 0xc8000247 Windows update 当前无法检查更新,因为未运行服务.您可能需要重新启动计算机. 问题现象:     在执行windows update进行操作系统更新的时候报错"Windows update 当前无法检查更新,因为未运行服务.您可能需要重新启动计算机",这个时候我对计算机进行了重起操作,问题是依旧没有解决.随即按照网上提供的办法将c:\ SoftwareDistribution 该目录删除,重起计算机,还是一样.后来我就收

框架-JAVA问题:删除数据库中数据的代码,测试不会报错,但实际什么都删不了

问题描述 JAVA问题:删除数据库中数据的代码,测试不会报错,但实际什么都删不了 这些操作做完后,数据库里的t_product表中,id=2的数据依然在,不是刷新的问题,刷新也还在 解决方案 为什么你的三个函数保存.修改.删除方法调用sql的时候都没有传递sql参数值呢? 解决方案二: 调用mapper里面的sql代码的时候,把要删除的id传进去了吗?不应该是sqlsession.delete("",参数);吗? 解决方案三: 参数没带.sqlsession.delect带上id 解决

hql-HQL中使用占位符查询mysql数据老是报错...

问题描述 HQL中使用占位符查询mysql数据老是报错... 执行语句如下: Session session = HibernateUtil.getSession(); String hql = "from Category where name=?"; Query query = session.createQuery(hql); query.setString(0, "饮品"); 错误: ava.lang.NoSuchMethodError: org.hiber