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

2024-05-29 10:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想在我的图形用户界面上做同样的事情,也就是说,它从我制作的图形用户界面输入,然后把文本提交到这个网站,然后得到源代码,这样我就不必每次从浏览器访问交叉检查了。在

提前谢谢


Tags: 文本http源代码htmlsoftware图形用户界面命名交叉
1条回答
网友
1楼 · 发布于 2024-05-29 10:41:46

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

首先你要做的是探索一点网站的源代码。 如果你看一下网站的源代码,你会看到这段代码

<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地址,因为我们已经在地址上了

^{pr2}$

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

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

但是我们要送什么去那里? 我们需要一个数据,数据是作为“paragraph”POST参数发送的,您可以看到,由于表单具有值POST的属性方法,而textarea的名称是“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(可选)解析这个内容变量 你提到你想用图形用户界面显示这个? 您可以使用GTK或Qt来实现这一点,并将此功能映射到单个按钮上,您必须读取一个tutorial,这样做非常容易。如果你有问题,请评论这篇文章,我可以用GUI扩展这个答案

相关问题 更多 >

    热门问题