坦率地说,我开始对涉及硒等产品的常规刮擦失去兴趣,除此之外我不确定它是否有效。这种方法确实可以。
只有当您有多个文件需要下载时,您才会这样做,至少以这种形式。
>>> import bs4
>>> form = '''<form method="POST" action="GAnnotation"><input name="a" value="" type="hidden"><input name="termUse" value="ancestor" type="hidden"><input name="relType" value="IPO=" type="hidden"><input name="customRelType" value="IPOR+-?=" type="hidden"><input name="protein" value="Q9BRY0" type="hidden"><input name="tax" value="" type="hidden"><input name="qualifier" value="" type="hidden"><input name="goid" value="" type="hidden"><input name="ref" value="" type="hidden"><input name="evidence" value="" type="hidden"><input name="with" value="" type="hidden"><input name="source" value="" type="hidden"><input name="q" value="" type="hidden"><input name="col" value="proteinDB,proteinID,proteinSymbol,qualifier,goID,goName,aspect,evidence,ref,with,proteinTaxon,date,from,splice" type="hidden"><input name="select" value="normal" type="hidden"><input name="aspectSorter" value="" type="hidden"><input name="start" value="0" type="hidden"><input name="count" value="25" type="hidden"><input name="format" value="gaf" type="hidden"><input name="gz" value="false" type="hidden"><input name="limit" value="22" type="hidden"></form>'''
>>> soup = bs4.BeautifulSoup(form, 'lxml')
>>> action = soup.find('form').attrs['action']
>>> action
'GAnnotation'
>>> inputs = soup.findAll('input')
>>> params = {}
>>> for input in inputs:
... params[input.attrs['name']] = input.attrs['value']
...
>>> import requests
>>> r = requests.post('http://www.ebi.ac.uk/QuickGO/GAnnotation', data=params)
>>> r
<Response [200]>
>>> open('temp.htm', 'w').write(r.text)
4082
如果您只需单击按钮,您就会收到下载的文件。
Chrome 浏览器的详细信息:
- 在 Chrome 中打开该页面。
- 右键单击“下载”链接。
- 选择“检查”。
- 在 Chrome 开发者菜单(靠近顶部)中选择“网络”,然后选择“全部”。
- 单击页面中的“下载”。
- --> 在新打开的窗口中单击“下载”。
- “quickgoUtil.js:36”将出现在“启动器”列中。
- 点击它。
- 现在您可以通过单击“form.submit();”的行号来设置断点。
- 再次点击“下载”;执行将在断点处暂停。
- 在右侧窗口中注意“本地”。其内容之一是“形式”。您可以将其扩展为表单的内容。
你想要的outerHTML
该元素的属性用于上面代码中使用的信息,即其action
和名称-值对。 (以及使用 POST 的隐含信息。)
现在使用请求模块向网站提交请求。
这是其中的项目列表params
如果您想提出其他要求。
>>> for item in params.keys():
... item, params[item]
...
('qualifier', '')
('source', '')
('count', '25')
('protein', 'Q9BRY0')
('format', 'gaf')
('termUse', 'ancestor')
('gz', 'false')
('with', '')
('goid', '')
('start', '0')
('customRelType', 'IPOR+-?=')
('evidence', '')
('aspectSorter', '')
('tax', '')
('relType', 'IPO=')
('limit', '22')
('col', 'proteinDB,proteinID,proteinSymbol,qualifier,goID,goName,aspect,evidence,ref,with,proteinTaxon,date,from,splice')
('q', '')
('ref', '')
('select', 'normal')
('a', '')