python - 我如何编写一个程序来单击Python中的特定链接

标签 python python-2.7 wxpython mechanize jython

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

示例:

  1. 网页:http://www.rcsb.org/pdb/home/home.do
  2. 搜索词:“1AW0”
  3. 在网站上搜索该词后,您将进入: http://www.rcsb.org/pdb/explore/explore.do?structureId=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

连同 2 个文件 1AW0.pdb1AW0.xml 一起保存到脚本目录(对于本例)。

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

关于python - 我如何编写一个程序来单击Python中的特定链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13784390/

相关文章:

python - Numpy:返回一个数组,其中 n 个条目最接近给定数字

wxPython:如何创建bash shell窗口?

python - 是否可以将 wx.Panel 定义为 Python 中的类?

python - matplotlib 为二元矩阵着色

python - 从 `scipy.ndimage.map_coordinates` 输出向量/数组

Python 似乎在完成对这些十六进制值的循环之前就要死了。怎么修?

python - 在 python 的自定义错误类中调用 super 有什么意义?

python - Python 2.7 中使用列表理解的多个 with 语句

javascript - 我的 Django 和 jQuery AJAX 表单提交设置有什么问题?

c# - 如何在 Python 中将所有方法调用委托(delegate)给 C# DLL