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

标签 python user-interface automated-tests urllib

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

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

我希望在我的 GUI 本身中完成同样的事情,即从我制作的 GUI 输入并将文本提交到此网站并获取源代码,以便进行交叉检查,我不必访问每个来自浏览器的时间。

提前致谢

最佳答案

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

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

<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>

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

http://text0.mib.man.ac.uk/software/geniatagger/

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

http://text0.mib.man.ac.uk/software/geniatagger/a.cgi

但是我们要向那里发送什么? 我们需要一个数据,数据作为“paragraph”POST 参数发送,您会看到,由于表单具有值为 POST 的属性方法,并且文本区域的名称是“paragraph”

我们使用此 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()

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

关于python - 一个Python脚本,可以自动在网站中输入一些文本并获取其源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8280157/

相关文章:

c++ - 具有 Python GUI 和 C++ 后台模块的混合应用程序

c# - 短时间显示文本层

java - 将 android UI xml 转换为代码

python - Wxpython 图像查看器 - 打开图像文件后菜单太紧

python - 文件大小总和的脚本

python - 属性错误 : 'SMOTE' object has no attribute 'fit_sample'

testing - 如何在测试中更加冗长?

unit-testing - 网站测试员的文件?

PHPUnit Selenium - 无法通过 CSS 找到元素

python - 设置缩放 QGraphicsItem 的变换点