2014-07-24 50 views
0

我試圖以編程方式將基因列表發送到知名網站DAVID(http://david.abcc.ncifcrf.gov/summary.jsp)進行功能註釋。儘管還有其他兩種方式 - API服務(http://david.abcc.ncifcrf.gov/content.jsp?file=DAVID_API.html)和網絡服務(http://david.abcc.ncifcrf.gov/content.jsp?file=WS.html),前者具有更嚴格的查詢限制,後者不接受我的ID類型(http://david.abcc.ncifcrf.gov/forum/viewtopic.php?f=14&t=885),所以唯一的選擇似乎是程序發佈表單,解析生成的頁面並提取下載鏈接。使用Firefox插件「是HttpFox」監控傳輸,我得到具有下列腳本一試:由python post與瀏覽器返回的頁面不同

import urllib 
import urllib2 
import requests as rq 
import time 

_n = 1 
url0 = 'http://david.abcc.ncifcrf.gov' 
url = 'http://david.abcc.ncifcrf.gov/summary.jsp' 
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:30.0) Gecko/20100101 Firefox/30.0' 

def get_cookie(session_id): # prepare 'Cookie' in the headers for the post 
    domain_hash = '260267544' # according to what's been sent by firefox 
    random_uid = '1113731634' # according to what's been sent by firefox 
    global _t0 
    init_time = _t0 
    global _t 
    prev_time = _t 
    _t = int(time.time()) 
    curr_time = _t 
    global _n 
    _n += 1 
    session_count = _n 
    campaign_count = 1 
    utma = '.'.join(str(x) for x in (domain_hash, random_uid, init_time, prev_time, curr_time, session_count)) 
    utmz = '.'.join(str(x) for x in (domain_hash, init_time, session_count, campaign_count, 'utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)')) 
    cookie = '; '.join(str(x) for x in ('__utma=' + utma, '__utmz=' + utmz, 'JSESSIONID=' + session_id)) 
    return(cookie) 

# first get the session ID 
_t = int(time.time()) 
_t0 = _t 
headers = {'User-Agent' : user_agent} 
r = rq.get(url, headers = headers) 
session_id = r.cookies['JSESSIONID'] 
cookie = get_cookie(session_id) 

# get the gene list 
gene = [] 
fh = open('list.txt', 'r') 
for line in fh: 
    gene.append(line.rstrip('\n')) 

fh.close() 

# then post the form 
headers = { # all below is according to what's been sent by firefox 
      'Host' : 'david.abcc.ncifcrf.gov', 
      'User-Agent' : user_agent, 
      'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
      'Accept-Language' : 'en-US,en;q=0.5', 
      'Accept-Encoding' : 'gzip, deflate', 
      'Referer' : url, 
      'Cookie': cookie, 
      'Connection' : 'keep-alive', 
#   'Content-Type' : 'multipart/form-data; boundary=---------------------------17914945481928137296675300642', 
#   'Content-Length' : '3581' 
      } 

data = { # all below is according to what's been sent by firefox 
     'idType' : 'OFFICIAL_GENE_SYMBOL', 
     'uploadType' : 'list', 
     'multiList' : 'false', 
     'Mode' : 'paste', 
     'useIndex' : 'null', 
     'usePopIndex' : 'null', 
     'demoIndex' : 'null', 
     'ids' : '\n'.join(gene), 
     'removeIndex' : 'null', 
     'renameIndex' : 'null', 
     'renamePopIndex' : 'null', 
     'newName' : 'null', 
     'combineIndex' : 'null', 
     'selectedSpecies' : 'null', 
     'SESSIONID' : session_id[-12:], # according to the pattern that the last 12 characters of 'JSESSIONID' is sent by firefox 
     'uploadHTML' : 'null', 
     'managerHTML' : 'null', 
     'sublist' : '', 
     'rowids' : '', 
     'convertedListName' : 'null', 
     'convertedPopName' : 'null', 
     'pasteBox' : '\n'.join(gene), 
     'fileBrowser' : '', 
     'Identifier' : 'OFFICIAL_GENE_SYMBOL', 
     'rbUploadType' : 'list'} 

r = rq.post(url = url, data = data, headers = headers) 
if r.status_code == 200: 
    fh = open("python.html", 'w') 
    fh.write(r.text) 
    fh.close() 

然而,我的代碼得到的頁面是272KB,由是HttpFox返回的內容,這是絕對不同428KB。我比較了頭部和我的劇本和火狐發送的形式,區別似乎只是爲了在

  1. 餅乾領域__utma和__utmz,但他們爲Google Analytics相關,這聽起來他們真的不應該在我評論的第二個標題中,字段的「內容類型」和「內容長度」是
  2. 。由於Is Python requests doing something wrong here, or is my POST request lacking something?中的建議,似乎沒有必要手動指定它們。但即使在我評論他們之後,它也不起作用。

以上是基本情況,我很感謝有人可以幫助解決問題的具體問題。此外,我還看到了其他一些建議,例如嘗試瀏覽器模擬器'機械化'。但是我對這個原因更加好奇,也就是說我的程序出了什麼問題,如果是這樣的話,怎麼改正呢,還是這些模塊根本不足以完成任務呢?非常感謝。

我的目錄發佈是:

Apba3 
Apoa1bp 
Dexi 
Dhps 
Dnpep 
Eral1 
Gcsh 
Git1 
Grtp1 
Guk1 
Ifrd2 
Lsm3 
Map2k1ip1 
Med31 
Mettl11a 
Mrpl2 
mrpl24 
Mrpl30 
Mrpl46 
Ndufaf3 
Nr1h2 
Obfc2b 
Parp3 
Pigt 
Pop5 
Ppt2 
Ptpmt1 
RGD1304567 
RGD1306215 
RGD1309708 
Rras 

我的瀏覽器後的程序是:

  1. 在Firefox中打開http://david.abcc.ncifcrf.gov/summary.jsp
  2. 在默認情況下,左側面板
  3. ,輸入在上述基因列表框「步驟1:輸入基因列表A:粘貼列表」
  4. 單擊下拉按鈕並在「步驟2:選擇標識符」中選擇「OFFICIAL_GENE_SYMBOL」
  5. 檢查單選按鈕「基因列表」,「第3步:列表類型」中
  6. 點擊「提交列表」「第4步:提交名單」

然後,瀏覽器返回一個新的頁面與彈出窗口提示用戶選擇物種和背景,這是httpFox在本文中追蹤的內容,也是我試圖通過腳本捕獲的內容。

回答

1

使用Selenium

from selenium import webdriver 
from time import sleep 

driver = webdriver.Firefox() 
driver.get('http://david.abcc.ncifcrf.gov/summary.jsp') 
sleep(0.1) 
query = """Apba3 
Apoa1bp 
Dexi 
Dhps 
Dnpep 
Eral1 
Gcsh 
Git1 
Grtp1 
Guk1 
Ifrd2 
Lsm3 
Map2k1ip1 
Med31 
Mettl11a 
Mrpl2 
mrpl24 
Mrpl30 
Mrpl46 
Ndufaf3 
Nr1h2 
Obfc2b 
Parp3 
Pigt 
Pop5 
Ppt2 
Ptpmt1 
RGD1304567 
RGD1306215 
RGD1309708 
Rras""" 
listBox = driver.find_element_by_id("LISTBox") 
listBox.send_keys(query) 

IDT = driver.find_element_by_id("IDT") 
IDT.send_keys("O") 

radioCheck = driver.find_element_by_name("rbUploadType") 
radioCheck.click() 


submitButton = driver.find_element_by_name("B52") 

submitButton.click() 
sleep(0.1) 
alert = driver.switch_to_alert() 
alert.accept() 
sleep(0.1) 
html = driver.page_source 

變量 「HTML」 中包含的頁面的源代碼。

+0

您好Caio,您的解決方案雖然可能會通過與外部Web瀏覽器通信而涉及一些開銷。如果沒有更好的人來,我會接受這個答案。無論如何,非常感謝。 – foehn

+0

不客氣。有時我遇到Firefox驅動器無響應的問題,如果您面對此嘗試[Chrome驅動程序](https://code.google.com/p/selenium/wiki/ChromeDriver)。 – caiohamamura

相關問題