如何用Python写一个抓取新浪财经网指定企业年报的脚本

2024-05-15

1. 如何用Python写一个抓取新浪财经网指定企业年报的脚本

代码如下: 1). xml可能的中文编码错误处理def xml_Error_C(filename):    fp_xml=open(filename)    fp_x=''#中文乱码改正    for i in range(os.path.getsize(filename)):        i+=1        a=fp_xml.read(1)        if a=='&':            fp_xml.seek(-1,1)            if fp_xml.read(6)==' ':                i+=5                continue            else:                fp_xml.seek(-5,1)        fp_x+=a    fp_xml=open(filename,'w+')    fp_xml.write(fp_x)    fp_xml.flush()    fp_xml.close() 2). xml转xlsdef Xmltoxls(xmlname,xlsname):    if os.path.getsize(xmlname)599999 else '02')+'&exp=1'        print 'I am handle '+line+' '+url.split()[1]+' '+'data for you'        filename=filepath+'\\'+line.split()[0]+' '+url.split()[1]+'.xls'        while True:#get xml data            try:                u=urllib2.urlopen(url_f)                time.sleep(0.3)                data=u.read()                f=open(temp,'w+')#保存文件                f.write(data)                f.flush()                f.close()                break            except :                print 'Network error,try latter!'                time.sleep(10)        while True:#xml data to xls data            if url.split()[1] in ['News','Notice','Subject']:                shutil.move(temp,filename) #   os.rename("oldname","newname")                break            try:                            xml_Error_C(temp)                Xmltoxls(temp,filename)            except IOError:                errorlog('No '+filename)            except:                shutil.move(temp,filename)                errorlog('Not Done '+filename)            break        time.sleep(0.2)    time.sleep(7)    fp_basic.seek(0)print 'All data have been getted.'fp_code.close()fp_basic.close()

如何用Python写一个抓取新浪财经网指定企业年报的脚本

2. 抓取新浪财经网的公司简介,右键查看网页源代码能看到公司简介,但用python却没有解析出!

url 发一下,有可能是js生成的数据。。

3. 如何用Python写一个抓取新浪财经网指定企业年报的脚本

#!/usr/bin/env python import sys str = True while (str): dig = int(input()) if dig > 100: print 'please input mun between 0~100' elif dig >= 90: print 'A' elif dig >= 80: print 'B' elif dig >= 70: print 'C' elif dig >= 60: prin

如何用Python写一个抓取新浪财经网指定企业年报的脚本

4. 如何用Python写一个抓取新浪财经网指定企业年报的脚本

#!/usr/bin/env python import sys str = True while (str): dig = int(input()) if dig > 100: print 'please input mun between 0~100' elif dig >= 90: print 'A' elif dig >= 80: print 'B' elif dig >= 70: print 'C' elif dig >= 60: prin

5. 写一个python脚本

index=True    while index:    score=input("请输入学生成绩(1-100,输入q退出程序):")    try:        if str(score)=="q":            index=False        else:            if int(score)>90 and int(score)80 and int(score)70 and int(score)=60 and int(score)<=70:                print "D"            elif int(score)<60 :                print "E"            else:                print "请输入正确的成绩!"    except:        print "请输入正确的标识符!"

写一个python脚本

6. Python 如何写脚本?

Python写脚本步骤如下:
1、首先下载安装python,建议安装2.7版本以上。

2、打开文本编辑器,推荐editplus,notepad等,将文件保存成 .py格式,editplus和notepad支持识别python语法。脚本第一行一定要写上 #!usr/bin/python,表示该脚本文件是可执行python脚本,如果您的python目录不在usr/bin目录下,则替换成当前python执行程序的目录。

3、编写完脚本之后注意调试、可以直接用editplus调试。调试方法可自行百度。脚本写完之后,打开CMD命令行,前提是python 已经被加入到环境变量中。

4、在CMD命令行中,输入 “python” + “空格”,即 ”python “;将已经写好的脚本文件拖拽到当前光标位置,然后敲回车运行即可。

7. 写一个简单的shell脚本或Python脚本

import osfor i in range(1, 21):	dir_Name = r'f:/27/other/py/File/File'+str(i)+'/'	os.makedirs(dir_Name)		for j in range(1, 4):		fileName = r'f:/27/other/py/File/File'+str(i)+'/'+'text'+str(j)+'.txt'		f = open(fileName,'w')		f.close()	for j in range(1, 21):		fileName = r'f:/27/other/py/File/File'+str(i)+'/'+str(j)+'text.txt'		f = open(fileName,'w')		f.close()你把dir_Name = r'f:/27/other/py/File/File'+str(i)+'/'这里的f:/27/other/py改成你File文件夹所在的位置就可以使用了。
File里面的File1-File20:

20个File里每个包含23个txt文件,名字按照你说的创建的。

写一个简单的shell脚本或Python脚本

8. 如何写一个多线程的python脚本

#coding=utf-8import threadingfrom time import ctime,sleepdef music(func):    for i in range(2):        print "I was listening to %s. %s" %(func,ctime())        sleep(1)def move(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(5)if __name__ == '__main__':    music(u'爱情买卖')    move(u'阿凡达')    print "all over %s" %ctime()
最新文章
热门文章
推荐阅读