2012-12-09 23 views
0

我的程序需要用戶輸入並通過特定的網頁進行搜索。此外,我希望它去和點擊一個特定的鏈接,然後下載目前在那裏的文件。我如何編寫一個程序來點擊Python中的特定鏈接

例子:

  1. 的網頁:http://www.rcsb.org/pdb/home/home.do
  2. 搜索詞: 「1AW0」
  3. 你搜索這個詞,它把你的網站後: http://www.rcsb.org/pdb/explore/explore.do?structureId=1AW0

我想讓程序進入網頁的右側,並從下載pdb文件下載文件選項

我已成功使用機械化模塊自動搜索這個詞卻無法找到一種方法,我可以點擊一個鏈接

我的代碼上寫一個程序:

import urllib2 
import re 
import mechanize 

br = mechanize.Browser() 
br.open("http://www.rcsb.org/pdb/home/home.do") 
## name of the form that holds the search text area 
br.select_form("headerQueryForm") 

## "q" name of the teaxtarea in the html script 
br["q"] = str("1AW0") 
response = br.submit() 
print response.read() 

任何幫助或者任何建議都會有幫助。

順便說一句我是中級程序員在Python中,我想學習Jython模塊嘗試做這項工作。

在此先感謝

+1

如果只是有關下載PDB文件對於給定的蛋白質,你爲什麼不只是使用http.client(或httplib的)下載http://www.rcsb.org/pdb /download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=HEREGOESTHEID。 (將鼠標懸停在此鏈接上可以完全看到)顯然,所有下載鏈接看起來都是一樣的。 – Hyperboreus

回答

1

下面是我會做:

''' 
Created on Dec 9, 2012 

@author: Daniel Ng 
''' 

import urllib 

def fetch_structure(structureid, filetype='pdb'): 
    download_url = 'http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=%s&compression=NO&structureId=%s' 
    filetypes = ['pdb','cif','xml'] 
    if (filetype not in filetypes): 
    print "Invalid filetype...", filetype 
    else: 
    try: 
     urllib.urlretrieve(download_url % (filetype,structureid), '%s.%s' % (structureid,filetype)) 
    except Exception, e: 
     print "Download failed...", e 
    else: 
     print "Saved to", '%s.%s' % (structureid,filetype) 

if __name__ == "__main__": 
    fetch_structure('1AW0') 
    fetch_structure('1AW0', filetype='xml') 
    fetch_structure('1AW0', filetype='png') 

其中規定,這樣的輸出:

Saved to 1AW0.pdb 
Saved to 1AW0.xml 
Invalid filetype... png 

隨着2個文件1AW0.pdb1AW0.xml其保存到腳本目錄(在這個例子中)。

http://docs.python.org/2/library/urllib.html#urllib.urlretrieve

+0

優秀的答案。 – Nodnin

+0

如何保存此文件並在沒有實際提供硬編碼位置的情況下進行檢索,我的意思是如果我必須在某人的計算機上運行此程序,並且必須檢索文件並對其進行計算,那該怎麼辦。 – Nodnin

+0

不知道我明白...你問的是如何改變他們下載的位置? – Ngenator

相關問題