如何在python中获取下一个表单内容 [英] How to get next form content in python

查看:78
本文介绍了如何在python中获取下一个表单内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在python中的第一个脚本(也是帖子).

This is my first script(also post) in python.

在脚本中,我填写了表单内容并提交.因此,提交表单后,它将在下一个表单上生成结果.现在的问题是下一个表格链接不是静态的,它将根据上一个表格中输入的数据进行更改.参见下面的脚本代码

In script i filled form content and submit it. So After submitting form it will generate result on next form. Now the issue is next form link not static, it will changed according data entered in previous form. See below some code of my script

import mechanize

browser = mechanize.Browser()

browser.open('https://example.com')

browser.select_form(nr=1)
browser.form["MyIDNO"] = '000D6F0004C46834'
browser.form["RuleID"] = '0109108301234567890A'
browser.submit()

以上代码仅填充数据并提交.现在我想要下一个打开的表单内容.我正在获得如下所示的动态链接

Above code just fill data and submit it. Now i want next opened form content. I am getting dynamic link as below

如上面的链接所示,它将基于MyIDNORuleID生成.

As seen in above link, it will generated based on MyIDNO and RuleID.

我尝试了以下一种解决方案

I tried one solution as below

html = browser.response().read()
print html

它将以html格式打印所有内容.现在我需要解析特定的数据.看到下面的一些输出

It will print all content in html form. Now i need to parse specific data. See below some output

<tr>
<td><strong>User key: </strong></td>
<td>0200fde8a7f3d1084224962a4e7c54e69ac3f04da6b8</td>
</tr>
<tr>
<td><strong>Institute id: </strong></td>
<td>
      030780ffa3641183273ad548ae09872f9dcf4b0c4267<br/>000d6f0004c468345445535453454341010910830123<br/>4567890a<br/> </td>
</tr>
<tr>
<td><strong>part id:</strong></td>
<td>00ecd01536ff66296f9d572219d7acac02d59b24c6</td>
</tr>
<tr>

从上面的内容中,我需要下面的输出

From above content i need below output

User key: 0200fde8a7f3d1084224962a4e7c54e69ac3f04da6b8
Institute id: 030780ffa3641183273ad548ae09872f9dcf4b0c4267000d6f0004c4683454455354534543410109108301234567890a
part id: 00ecd01536ff66296f9d572219d7acac02d59b24c6

推荐答案

一旦您有了html文档,就可以使用 BeautifulSoup 来获取所需的数据.

Once you have the html document you can use BeautifulSoup for getting the data you need.

from bs4 import BeautifulSoup

# submit form as per your snippet

html = browser.response().read()
soup = BeautifulSoup(html, 'html.parser')

# Process the content with BeautifulSoup. 

这篇关于如何在python中获取下一个表单内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆