如何通过抓取从ucsc基因组浏览器中提取表浏览器结果

时间:2015-01-14 17:25:16

标签: python web-scraping beautifulsoup mechanize

我想自动化从UCSC基因组浏览器中提取信息的过程,以节省大量的手动输入。以下代码获取我认为是表单的正确部分,但我无法提取结果:

 from mechanize import Browser
 from bs4 import BeautifulSoup
 import requests
 import re

 br = Browser()
 url = 'http://genome-euro.ucsc.edu/cgi-bin/hgTables?hgsid=201790284_dkwVYFu7V6ISmTzFGlXzo23aUhXk'
 br.set_handle_robots( False )

 for form in br.forms():
      if form['position']:
           print form['position']
           form['position'] = 'chr9:21802635-21865969'
           print form['position']
           break

 for form in br.forms():
     if form['hgta_doTopSubmit']:
          br.submit()

第一部分工作正常,改变了要查询的基因组的位置。第二部分似乎没有提交任何内容。它返回此错误:

追踪(最近一次通话):   文件" C:\ Documents and Settings \ Silvia \ Desktop \ Copy \ web_scraping \ UCSC \ table_browser_form.py",第26行,in     br.submit()   文件" C:\ Python27 \ lib \ site-packages \ mechanize-0.2.5-py2.7.egg \ mechanize_mechanize.py",第541行,提交     return self.open(self.click(* args,** kwds))   文件" C:\ Python27 \ lib \ site-packages \ mechanize-0.2.5-py2.7.egg \ mechanize_mechanize.py",第530行,点击     request = self.form.click(* args,** kwds) AttributeError:' NoneType'对象没有属性'点击'

手动网站会返回与此位置详细信息对应的文本屏​​幕,但我似乎无法在此处提取。有没有人有任何建议如何解决这个问题?我只需要存储输出结果 - 相当于用户按下“获得结果”#39;在网站上。非常感谢。

1 个答案:

答案 0 :(得分:1)

您始终可以使用requests

发出请求
import requests

url = 'http://genome-euro.ucsc.edu/cgi-bin/hgTables?hgsid=201790284_dkwVYFu7V6ISmTzFGlXzo23aUhXk'    
session = requests.Session()

params = {
    'hgsid': '201790284_dkwVYFu7V6ISmTzFGlXzo23aUhXk',
    'jsh_pageVertPos': '0',
    'clade': 'mammal',
    'org': 'Human',
    'db': 'hg19',
    'hgta_group': 'genes',
    'hgta_track': 'refGene',
    'hgta_table': 'refFlat',
    'hgta_regionType': 'range',
    'position': 'chr9:21802635-21865969',
    'hgta_outputType': 'gff',
    'boolshad.sendToGalaxy': '0',
    'boolshad.sendToGreat': '0',
    'boolshad.sendToGenomeSpace': '0',
    'hgta_outFileName': '',
    'hgta_compressType': 'none',
    'hgta_doTopSubmit': 'get output'
}

response = session.post(url, data=params)
print response.content

打印:

chr9    hg19_refFlat    start_codon 21802748    21802750    0.000000    +   .   gene_id "MTAP"; transcript_id "MTAP"; 
chr9    hg19_refFlat    CDS 21802748    21802780    0.000000    +   0   gene_id "MTAP"; transcript_id "MTAP"; 
chr9    hg19_refFlat    exon    21802635    21802780    0.000000    +   .   gene_id "MTAP"; transcript_id "MTAP"; 
chr9    hg19_refFlat    CDS 21815432    21815518    0.000000    +   0   gene_id "MTAP"; transcript_id "MTAP"; 
...
chr9    hg19_refFlat    exon    21861975    21865969    0.000000    +   .   gene_id "MTAP"; transcript_id "MTAP";