坦率地说,我开始对涉及硒等产品的常规抓取失去兴趣,然后我不确定它是否会起作用。这种方法可以。
如果您有多个文件要下载,您只会这样做,至少在这种形式下。
>>> 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 _Developer_ 菜单(靠近顶部)中选择“网络”,然后选择“全部”。
- 点击页面中的“下载”。
- --> 在新打开的窗口中单击“下载”。
- “quickgoUtil.js:36”将出现在“启动器”列中。
- 点击它。
- 现在您可以在 `form.submit();` 上设置断点,方法是单击其行号。
- 再次点击“下载”;执行将在断点处暂停。
- 在右侧窗口中通知“本地”。它的内容之一是`form`。您可以将其展开为表单的内容。
您需要此元素的 outerHTML 属性来获取上面代码中使用的信息,即其 action 和名称-值对。 (以及使用 POST 的隐含信息。)
现在使用 requests 模块向网站提交请求。
这是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', '')