python - 使用 Python 进行自动化数据挖掘的建议

标签 python automation

我是一名生物学家,具有一点 Python 编程经验。我的研究方法之一涉及使用此数据库分析大型基因列表:https://david.ncifcrf.gov/ 谁能告诉我是否可以对输出进行关键字搜索并返回与关键字关联的基因名称?这是“表格”输出,看起来是这样的:https://david.ncifcrf.gov/annotationReport.jsp?annot=59,12,87,88,30,38,46,3,5,55,53,70,79&currentList=0 还有后端和 api 选项。 非常感谢所有见解和建议。

最佳答案

如果有一个 API 可以为您提供所有数据,您几乎可以自动化与之相关的所有内容。 API 是 REST 或 SOAP,因此您首先需要弄清楚您需要什么。

如果 API 是 RESTful:

import urllib2, json

url = "https://mysuperapiurl.com/api-ws/api/port/" 
u = 'APIUsername'
p = 'APIPassword'

def encodeUserData(user, password):
    return "Basic " + (user + ":" + password).encode("base64").rstrip()
req = urllib2.Request(url)
req.add_header('Accept', 'application/json')
req.add_header("Content-type", "application/x-www-form-urlencoded")
req.add_header('Authorization', encodeUserData(u, p))
res = urllib2.urlopen(req)
j = json.load(res) # Here is all the data from the API
json_str= json.dumps(j) # this is the same as above as string

如果 API 是 SOAP,它会变得有点困难。我推荐的是zeep .如果因为您的服务器是 2.6 或者因为有几个人正在使用它而无法做到这一点,那么请使用 suds

使用泡沫的 API 调用如下所示:

import logging, time, requests, re, suds_requests
from datetime import timedelta,date,datetime,tzinfo
from requests.auth import HTTPBasicAuth
from suds.client import Client
from suds.wsse import *
from suds import null
from cStringIO import StringIO
from bs4 import BeautifulSoup as Soup

log_stream = StringIO()
logging.basicConfig(stream=log_stream, level=logging.INFO)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

WSDL_URL = 'http://213.166.38.97:8080/SRIManagementWS/services/SRIManagementSOAP?wsdl'

username='username'
password='password'
session = requests.session()
session.auth=(username, password)

def addSecurityHeader(client,username,password):
    security=Security()
    userNameToken=UsernameToken(username,password)
    security.tokens.append(userNameToken)
    client.set_options(wsse=security)

addSecurityHeader(client,username,password)

arg1 = "argument_1"
arg2 = "argument_2"

try:
    client.service.GetServiceById(arg1, arg2)
except TypeNotFound as e:
    print e
logresults = log_stream.getvalue()

您将收到 xml 作为返回,所以我使用 beautifulsoup 来美化结果:

soup = Soup(logresults)
print soup.prettify()

好的,API 连接部分已经涵盖,您将数据存储在哪里,以及您在哪里迭代这些数据以执行关键字搜索?在你的数据库中。我推荐 MySQLdb。设置表格并考虑要将哪些信息(从 API 收集)存储在哪一列中。

def dbconnect():
    try:
        db = MySQLdb.connect(
            host='localhost',
            user='root',
            passwd='password',
            db='mysuperdb'
        )
    except Exception as e:
        sys.exit("Can't connect to database")
    return db

def getSQL():   
    db = dbconnect()
    cursor = db.cursor()
    sql = "select * from yoursupertable"
    dta = cursor.execute(sql)
    results = cursor.fetchall()
    return results

def dataResult():
    results = getSQL()
    for column in results:
        id = (column[1])
print dataResult()

所以这是你设置关键字的地方(也可以通过另一个 SQL 来完成)并将你从数据库中提取的结果与列表、字典、文本文件或硬编码关键字进行比较,并定义如果它们匹配等时要做什么 :)

关于python - 使用 Python 进行自动化数据挖掘的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739786/

相关文章:

python - 尝试使用 mysqldump 导出数据库时,是什么导致 subprocess.call 输出空白文件?

python - 当我在 Google App Engine/Python Production 的请求 url 路径中使用 %2F 时返回 404

javascript - Protractor - Jasmine 。仅当存在特定元素时才执行某些操作。

python - 如何自动更新 trac wiki 页面?

python - 神经网络不学习(损失保持不变)

python - Plaid Oauth redirect_uri 未配置错误

python - 将python测试运行信息导入sonarqube

eclipse - 使用所有插件引导新的 Eclipse 机器

perl - 执行 perl 测试的时间指定是多少

compilation - 为 AutoCAD Lisp 文件构建自动化