关闭

python练习:疾病专家信息获取工具

标签: python界面utf-8csv
211人阅读 评论(0) 收藏 举报
分类:
  • 做界面如下,编辑信号/槽拉确定到空白,选定accept()

这里写图片描述

  • 存为test.ui。

  • 命令提示符,pyuic4 test.ui -o test.py,转为py文件。

  • 主程序这样写:
# -*- coding: utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
import re
import chardet
import csv
########################################
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import test
class TestDialog(QDialog,test.Ui_Dialog):  
    def __init__(self,parent=None):  
        super(TestDialog,self).__init__(parent)  
        self.setupUi(self)

app=QApplication(sys.argv)  
dialog=TestDialog()  
dialog.show()  
app.exec_()  
########################################
disease=unicode(dialog.lineEdit.text())
#disease='environmental+illness'
url ='http://expertscape.com/ex/'+disease  
req = urllib2.Request(url)
response = urllib2.urlopen(req)
result = response.read()
response.close()
soup = BeautifulSoup(result)

########################################
country_td= soup.find_all(id="expColTD_c")
country_text = str(country_td)
r_country = re.compile(r',(\d+),".*?,"(.*?)"\)')
country = r_country.findall(country_text)
begin_country=int(country[0][0]);
end_country=int(country[-1][0]);
########################################
city_td= soup.find_all(id="expColTD_t")
city_text = str(city_td)
r_city = re.compile(r',(\d+),".*?,"(.*?)"\)')
city = r_city.findall(city_text)
begin_city=int(city[0][0]);
end_city=int(city[-1][0]);
########################################
institution_td= soup.find_all(id="expColTD_i")
institution_text = str(institution_td)
r_institution = re.compile(r',(\d+),".*?,"(.*?)"\)')
institution = r_institution.findall(institution_text)
begin_institution=int(institution[0][0]);
end_institution=int(institution[-1][0]);
########################################
expert_td= soup.find_all(id="expColTD_a")
expert_text = str(expert_td)
r_expert = re.compile(r',(\d+),".*?,"(.*?)"\)')
expert = r_expert.findall(expert_text)
begin_expert=int(expert[0][0]);
end_expert=int(expert[-1][0]);
########################################
csv = ['SN, FN,CITY,COUNTRY,INSTITUTION\n']
file_name= disease+'_experts.csv'
file_object = open(file_name, 'w+')
for i in range(begin_expert,end_expert+1):
    Et=expert[i- begin_expert][1]
    r_match_indexes_s = re.compile('z\['+str(i)+'\]=\[(.*?)\]')
    match_indexes_s=r_match_indexes_s.findall(result)[0]
    r_city_index=re.compile('')
    match_indexes_n = [int(k) for k in match_indexes_s.split(',')]#这个表达式牛逼
    Cy  = 'none' 
    Cty = 'none'
    In= 'none'
    for index in match_indexes_n:      
        if index>=begin_city and index<=end_city:
            if Cy == 'none':
                Cy=city[index-begin_city][1]
            else:
                Cy=Cy+'&'+city[index-begin_city][1] 
        if   index>=begin_country and index<=end_country and country[index-begin_country][1]!='Worldwide'\
        and country[index-begin_country][1]!='Unspecified' and country[index-begin_country][1]!='Asia'\
        and  country[index-begin_country][1]!='Europe':
            if Cty == 'none':
                Cty=country[index-begin_country][1]
            else:
                Cty=Cty+'&'+country[index-begin_country][1] 
        if  index>=begin_institution and index<=end_institution:
            if In == 'none':
                In=institution[index-begin_institution][1]
            else:
                In=In+'&'+institution[index-begin_institution][1]            
    info=Et+','+Cy.replace(",","_")+','+Cty.replace(",","_")+','+In.replace(",","_")+'\n'
    info=info.decode('utf-8').encode('mbcs')
    csv.append(info)
file_object.writelines(csv)
file_object.close()



  • 转成exe===>>>因用了pyQT做界面,setup.py这样写:
from distutils.core import setup
import py2exe
setup(console=[{"script":"英文式主程序名.py"}], options={"py2exe":{"includes":["sip"]}})


打开终端运行setup.py py2exe ,咻的一下就出来了。

重复性工作,电子记录,做个存档而已。哪里都是 随意,随意。。。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:227703次
    • 积分:4161
    • 等级:
    • 排名:第8323名
    • 原创:201篇
    • 转载:1篇
    • 译文:0篇
    • 评论:38条
    博客专栏
    最新评论