一个python脚本,可以自动在网站中输入一些文本并获取其源代码

Md *_*sal 2 python user-interface automated-tests urllib

我正在使用Python进行生物医学命名提取.

现在我必须交叉检查从输入文本到http://text0.mib.man.ac.uk/software/geniatagger/的结果,并解析我在提交文本后得到的HTML文本的源代码.

我想在我的GUI本身做同样的事情,即它从我做的GUI输入并将文本提交到这个网站并获取源代码,以便交叉检查我不必每次访问浏览器.

提前致谢

Jan*_*cak 5

实际上,这是一个很好的问题!

您要做的第一件事是稍微探索一下网站的源代码.如果你查看网站的源代码,你会看到这段代码

<form method="POST" action="a.cgi">
<p>
Please enter a text that you want to analyze.
</p>
<p>
<textarea name="paragraph" rows="15" cols="80" wrap="soft">
... some text here ...
### This is a sample. Replace this with your own text.

</textarea>
</p>
<p>
<input type="submit" value="Submit Text" />
<input type="reset" />
</p>
</form>
Run Code Online (Sandbox Code Playgroud)

您看到的是该请求被发送到a.cgi地址,因为我们已经在地址上

http://text0.mib.man.ac.uk/software/geniatagger/
Run Code Online (Sandbox Code Playgroud)

我们要发送的数据将发送到与此连接的地址

http://text0.mib.man.ac.uk/software/geniatagger/a.cgi
Run Code Online (Sandbox Code Playgroud)

但是我们要送什么呢?我们需要一个数据,数据作为"段落"POST参数发送,你看到因为表单有属性方法,值为POST,而textarea的名称是"段落"

我们使用这个python代码打开它

import urllib
import urllib2

text =  """
        Further, while specific constitutive binding to the peri-kappa B site is seen in monocytes, stimulation with phorbol esters induces additional, specific binding. Understanding the monocyte-specific function of the peri-kappa B factor may ultimately provide insight into the different role monocytes and T-cells play in HIV pathogenesis. 

### This is a sample. Replace this with your own text.
        """
data = {
        "paragraph" : text 
       }

encoded_data = urllib.urlencode(data)
content = urllib2.urlopen("http://text0.mib.man.ac.uk/software/geniatagger/a.cgi",
        encoded_data)
print content.readlines()
Run Code Online (Sandbox Code Playgroud)

到目前为止我们得到了什么?我们为您的GUI程序提供了一个"引擎".你可以做的是用python的HTMLParser解析这个内容变量(可选)你提到你想在GUI中显示它吗?您可以使用GTK或Qt执行此操作并将此功能映射到单个按钮,您必须阅读教程,这很容易实现此目的.如果你有问题只是评论这篇文章,我可以用GUI扩展这个答案