Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写程序来单击Python中的特定链接_Python_Python 2.7_Wxpython_Mechanize_Jython - Fatal编程技术网

如何编写程序来单击Python中的特定链接

如何编写程序来单击Python中的特定链接,python,python-2.7,wxpython,mechanize,jython,Python,Python 2.7,Wxpython,Mechanize,Jython,我的程序接受用户输入并通过特定网页进行搜索。此外,我希望它去点击一个特定的链接,然后下载文件存在那里 例如: 网页: 搜索词:“1AW0” 在网站上搜索单词后,您将进入: 我希望程序位于网页的右侧,并从下载文件选项下载pdb文件 我已经设法写了一个程序,使用mechanize模块自动搜索这个词,但找不到一种方法,我可以点击一个链接 我的代码: import urllib2 import re import mechanize br = mechanize.Browser() br.open(

我的程序接受用户输入并通过特定网页进行搜索。此外,我希望它去点击一个特定的链接,然后下载文件存在那里

例如:

  • 网页:
  • 搜索词:“1AW0”
  • 在网站上搜索单词后,您将进入:
  • 我希望程序位于网页的右侧,并从下载文件选项下载pdb文件

    我已经设法写了一个程序,使用mechanize模块自动搜索这个词,但找不到一种方法,我可以点击一个链接

    我的代码:

    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模块,以使其能够工作


    提前感谢

    以下是我应该怎么做的:

    '''
    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
    
    以及保存到脚本目录的两个文件
    1AW0.pdb
    1AW0.xml
    (本例中)


    以下是我应该如何做到的:

    '''
    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
    
    以及保存到脚本目录的两个文件
    1AW0.pdb
    1AW0.xml
    (本例中)


    如果只是下载给定蛋白质的pdb文件,为什么不使用http.client(或httplib)下载呢。(将鼠标悬停在该链接上以查看完整内容)显然,所有下载链接看起来都完全相同。如果只是下载给定蛋白质的pdb文件,为什么不使用http.client(或httplib)下载呢。(将鼠标悬停在该链接上以查看完整内容)显然所有下载链接看起来都完全相同。我如何保存并检索该文件而不实际提供硬编码位置,我的意思是,如果我必须在其他人的计算机上运行该程序,并且必须检索该文件并在其上进行计算,我不确定我是否理解。。。您是否在询问如何更改下载位置?是的,如果必须请求输入并下载文件,我会将其分配给变量x=“1AW0”。并将其用作str(x)来获得下载。用户也会这样做。但该文件将在用户PC上下载。我需要访问该文件,因为程序的下一部分将让我计算该文件。。我怎样才能做到这一点呢?好吧,你可以把它作为另一个变量加入,然后询问用户。该函数将文件名作为第二个参数。只需在开头添加一个路径,它就会将文件保存在那里,而不是脚本的目录。我如何保存并检索此文件而不实际提供硬编码位置,我的意思是,如果我必须在其他人的计算机上运行此程序,并且必须检索该文件并在其上进行计算,该怎么办?我不确定我是否理解。。。您是否在询问如何更改下载位置?是的,如果必须请求输入并下载文件,我会将其分配给变量x=“1AW0”。并将其用作str(x)来获得下载。用户也会这样做。但该文件将在用户PC上下载。我需要访问该文件,因为程序的下一部分将让我计算该文件。。我怎样才能做到这一点呢?好吧,你可以把它作为另一个变量加入,然后询问用户。该函数将文件名作为第二个参数。只需在开头添加一个路径,它就会将文件保存在那里,而不是脚本的目录。