在这里,我们将给大家分享关于如何用Jsoup填写表格?的知识,同时也会涉及到如何更有效地android中用jsoup提取html表格的问题、javascript–如何在填写表格后永久禁用按钮?、jav
在这里,我们将给大家分享关于如何用Jsoup填写表格?的知识,同时也会涉及到如何更有效地android中用jsoup提取html表格的问题、javascript – 如何在填写表格后永久禁用按钮?、javascript – 如何用JSON填写表单?、javascript-如何用js添加表格?的内容。
本文目录一览:- 如何用Jsoup填写表格?
- android中用jsoup提取html表格的问题
- javascript – 如何在填写表格后永久禁用按钮?
- javascript – 如何用JSON填写表单?
- javascript-如何用js添加表格?
如何用Jsoup填写表格?
我试图浏览到加利福尼亚网站http://kepler.sos.ca.gov/的描述页面。但无法走。
然后,我有一个html表单,在该表单上我正在提交请求,我无法在此处添加表单,但是它只是带有必需参数的POST
对http://kepler.sos.ca.gov/的请求
我能得到__EVENTTARGET
并且__EVENTARGUMENT
从我来到这里之前的页面。
我究竟做错了什么?
码:
String url = "kepler.sos.ca.gov/";Connection.Response resp = Jsoup.connect(url) .timeout(30000) .method(Connection.Method.GET) .execute();Document responseDocument = resp.parse();Map<String, String> loginCookies = resp.cookies(); eventValidation=responseDocument.select("input[name=__EVENTVALIDATION]").first();viewState = responseDocument.select("input[name=__VIEWSTATE]").first();
答案1
小编典典您要使用FormElement
。这是Jsoup的有用功能。它能够找到在表单内声明的字段并将其发布给您。发布表单之前,您可以使用Jsoup
API设置字段的值。
注意:
在下面的示例代码中,您将始终看到对Element#select方法的调用,以及对Elements#first方法的调用。
例如 :
responseDocument.select("form#aspnetForm").first()
Jsoup
1.11.1引入了更有效的替代方法:Element#selectFirst。您可以将其用作原始替代品的直接替代品。例如:
responseDocument.select("form#aspnetForm").first()
可以替换为responseDocument.selectFirst("form#aspnetForm")
样本代码
// * Connect to websiteString url = "http://kepler.sos.ca.gov/";Connection.Response resp = Jsoup.connect(url) // .timeout(30000) // .method(Connection.Method.GET) // .execute();// * Find the formDocument responseDocument = resp.parse();Element potentialForm = responseDocument.select("form#aspnetForm").first();checkElement("form element", potentialForm);FormElement form = (FormElement) potentialForm;// * Fill in the form and submit it// ** Search TypeElement radioButtonListSearchType = form.select("[name$=RadioButtonList_SearchType]").first();checkElement("search type radio button list", radioButtonListSearchType);radioButtonListSearchType.attr("checked", "checked");// ** Name searchElement textBoxNameSearch = form.select("[name$=TextBox_NameSearch]").first();checkElement("name search text box", textBoxNameSearch);textBoxNameSearch.val("cali");// ** Submit the formDocument searchResults = form.submit().cookies(resp.cookies()).post();// * Extract results (entity numbers in this sample code)for (Element entityNumber : searchResults.select("table[id$=SearchResults_Corp] > tbody > tr > td:first-of-type:not(td[colspan=5])")) { System.out.println(entityNumber.text());}public static void checkElement(String name, Element elem) { if (elem == null) { throw new RuntimeException("Unable to find " + name); }}
输出(截至撰写本文时)
C3036475C3027305C3236514C3027304C3034012C3035110C3028330C3035378C3124793C3734637
也可以看看:
在此示例中,我们将使用FormElement类登录GitHub网站。
// # Constants used in this examplefinal String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; final String LOGIN_FORM_URL = "https://github.com/login";final String USERNAME = "yourUsername"; final String PASSWORD = "yourPassword";// # Go to login pageConnection.Response loginFormResponse = Jsoup.connect(LOGIN_FORM_URL) .method(Connection.Method.GET) .userAgent(USER_AGENT) .execute();// # Fill the login form// ## Find the form first...FormElement loginForm = (FormElement)loginFormResponse.parse() .select("div#login > form").first();checkElement("Login Form", loginForm);// ## ... then "type" the username ...Element loginField = loginForm.select("#login_field").first();checkElement("Login Field", loginField);loginField.val(USERNAME);// ## ... and "type" the passwordElement passwordField = loginForm.select("#password").first();checkElement("Password Field", passwordField);passwordField.val(PASSWORD);// # Now send the form for loginConnection.Response loginActionResponse = loginForm.submit() .cookies(loginFormResponse.cookies()) .userAgent(USER_AGENT) .execute();System.out.println(loginActionResponse.parse().html());public static void checkElement(String name, Element elem) { if (elem == null) { throw new RuntimeException("Unable to find " + name); }}
所有表单数据都由FormElement类为我们处理(甚至包括表单方法检测)。调用FormElement#submit方法时,将建立一个现成的Connection。我们要做的就是用附加头(cookie,用户代理等)完成此连接并执行它。
android中用jsoup提取html表格的问题
新手一枚
我需要提取的html是表格形式的,我现在想要用jsoup把表格的内容提取出来,放在一个listview或者是textview
<table width="100%" border="" cellspacing="5"cellpadding="5"> <tr valign="top"> <td width="500" align="left"> <div align="left"><b>Semester Timetable for<font color="#0000FF">ZHANG YAXIN</font></b></div> </td> <td width="250" align="right"><b>Acad Year:<fontcolor="#0000FF">2011/2012</font></b></td> <td width="100" align="right"><b>Sem: <fontcolor="#0000FF">Apr</font></b></td> </tr> <tr> <td colspan="3"> <table width="100%" border=""cellspacing="" cellpadding="2"> <tr class=tt_title> <td background="images/curveleft.gif"width="5%" align="left"> Day </td> <td width="1%"> </td> <td width="5%" align="left">Start</td> <td width="1%"> </td> <td width="5%">Finish</td> <td width="1%"> </td> <td width="5%" align="left"> Type </td> <td width="1%"> </td> <td width="5%" align="left"> Subject</td> <td width="1%"> </td> <td width="20%" align="left"> Lecturer</td> <td width="1%"> </td> <td width="15%" align="left"> Room</td> <td width="1%"> </td> <td width="10%" align="left"> Weeks</td> <td width="1%"> </td> <td width="12%" align="left"> Group</td> <td width="1%"background="images/curveright.gif"> </td> </tr>
目前我的代码只是写到这样
package sg.edu.tp.iit.mns; import org.w3c.dom.Document; import android.app.Activity; import android.os.Bundle; import android.sax.Element; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class TtestMain extends Activity { EditText editText1; Button enterBtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText1 = (EditText)findViewById(R.id.editText1); enterBtn = (Button)findViewById(R.id.enterBtn); enterBtn.setOnClickListener(listener); } View.OnClickListener listener = new View.OnClickListener() { public void onClick(View v) { String str = editText1.getText().toString(); if(v == enterBtn) { TextView tv = new TextView( this ); String myString = null ; StringBuffer sff = new StringBuffer(); try { Document doc = Jsoup.connect( "http://epoly.tp.edu.sg/tpapp/isistt/TTServlet?txtModule=StudentSearch&txtAction=GetTT&txtSelStudentID=" + str ).get(); } myString = sff.toString(); } catch (Exception e) { myString = e.getMessage(); e.printStackTrace(); } tv.setText(myString); this .setContentView(tv); } }; };}
中间提取的部分只要写进去element之类的就会有错误出现
请各位高手们帮帮忙!
javascript – 如何在填写表格后永久禁用按钮?
<div> <div> <div> <table> <tr> <td>Product</td> <td><a href="#">RESERVE Now</a></td> </tr> </table> <form> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit"> </form> </div> </div> </div>
我已经嘲笑了一个可以在这里找到的例子:
https://jsfiddle.net/rman215/gw2m53w9/1/
解决方法
因此,您可以在数据库中存储标志(例如,在disabled_at [timestamp,default = null]列中).当有人点击按钮并调用处理页面(通过ajax或简单重定向)时,设置标志disabled_at值Now().并且每次处理页面都是disabled_at IS NULL.如果是 – 将名字和姓氏保存到数据库.
如果disabled_at IS NOT NULL,则在具有表单的页面上需要添加属性以禁用提交按钮
编辑:
disable.PHP
<?PHP function reserveItem($name = null,$value = null) { $name .= '.reserved'; $value = $value ? $value : 1; return file_put_contents($name,$value); } function isdisabled($name = null) { return file_exists($name . '.reserved'); } $disabled = isdisabled('item1'); if (isset($_POST['submit'])) { $name = isset($_POST['firstname']) ? $_POST['firstname'] : ''; $lastName = isset($_POST['lastname']) ? $_POST['lastname'] : ''; if (!$disabled) { reserveItem('item1',$name . $lastName); } } ?> <!DOCTYPE html> <html> <head> <Meta charset="utf-8"> </head> <body> <div> <div> <div> <table> <tr> <td>Product</td> <td><a href="#">RESERVE Now</a></td> </tr> </table> <form method="post" action="disable.PHP"> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit" <?= $disabled ? 'disabled' : ''; ?>> </form> </div> </div> </div> </body> </html>
注意:代码仅在编辑器中编写而未经过测试.
javascript – 如何用JSON填写表单?
JSON:
{ "id" : 12,"name": "Jack","description": "Description" }
填写表格
<form> <input type="text" name="id"/> <input type="text" name="name"/> <input type="text" name="description"/> </form>
解决方法
虽然这是您拥有的唯一用例,但您也可以手动执行此操作.
javascript-如何用js添加表格?
我想通过JavaScript在html表格上添加表格.
我已经尝试使用appendChild和insertBefore方法,但是这些方法无效.
这是我的JavaScript:
var utilisateur = [{
id: 51,checked: false,prenom: "Dolores",date: "Fermière",examen: "host",note: "ww.dolores@gmail.com"
},{
id: 52,checked: true,prenom: "Bernard",date: "Robopsycologue",examen: "human",note: "ww.bernard@gmail.com"
}
// {
// id: 3,// name: "Robert",// job: "Directeur",// specie: "human",// email: "ww.robert@gmail.com"
// },// {
// id: 4,// name: "Maeve",// job: "Maquerelle",// specie: "host",// email: "ww.maeve@gmail.com"
// },// {
// id: 5,// name: "Teddy",// job: "Inconnu",// email: "ww.teddy@gmail.com"
// },// {
// id: 6,// name: "William",// job: "Actionnaire",// email: "ww.william@gmail.com"
// },// {
// id: 7,// name: "Elsie",// job: "Programmeuse",// email: "ww.elsie@gmail.com"
// },// {
// id: 8,// name: "Nathanael",// job: "Dev",// specie: "human",// email: "s.nathanael@outlook.fr"
// }
];
for (let i = 0; i < 100; i++) {
var row = document.createElement("tr");
var cell0 = document.createElement("td");
var cell1 = document.createElement("td");
var atr = document.createAttribute("class");
atr.value = "bs-checkBox";
cell1.setAttributeNode(atr);
var para = document.createElement("input");
var para1 = document.createAttribute("data-index");
var para2 = document.createAttribute("name");
var para3 = document.createAttribute("type");
para1.value = "0";
para2.value = "btSelectItem";
para3.value = "checkBox";
para.setAttributeNode(para1);
para.setAttributeNode(para2);
para.setAttributeNode(para3)
cell1.appendChild(para);
// var cell1 = '<tdhttps://www.jb51.cc/tag/Box/" target="_blank">Box"></td>'
//alert(cell1).innerText;
var cell2 = document.createElement("td");
var cell3 = document.createElement("td");
var cell4 = document.createElement("td");
var cell5 = document.createElement("td");
var cell6 = document.createElement("td");
var cell7 = document.createElement("td");
var cell8 = document.createElement("td");
cell0.innerText = utilisateur[i].id;
cell1.innerText = utilisateur[i].checked
cell2.innerText = utilisateur[i].prenom;
cell3.innerText = utilisateur[i].date;
cell4.innerText = utilisateur[i].examen;
cell5.innerText = utilisateur[i].note;
row.appendChild(cell0);
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
// row.appendChild(cell6);
// row.appendChild(cell7);
// row.appendChild(cell8);
// document.getElementsById("tbody")[0].appendChild(row);
// var elem = document.getElementsById("tabs1");
// var mychild = document.getElementById("")
// elem.insertBefore(mypara,mychild);
// elem.appendChild(row);
var element = document.getElementById("tabs1")[0];
element.appendChild(row);
}
$(document).ready(function() {
$(cell1).toggleClass("bs-checkBox");
});
这是我的HTML:
<divid="tabs1">
<!-- <table>
<thead>
<tr>
<th scope="col">#</th>
<th>Name</th>
<th>Job</th>
<th>Attribut</th>
<th>Email</th>
</tr>
</thead>
</table> -->
</div>
<tabledata-click-to-select="true" data-filter-control="true" data-search="true" data-show-export="true" data-toggle="table" data-toolbar="#toolbar" id="table">
<thead>
<tr>
<th id="0"https://www.jb51.cc/tag/dis/" target="_blank">display: none;"></th>
<th data-checkBox="true" data-field="state"></th>
<th data-field="prenom" data-filter-control="input" data-sortable="true">Prénom</th>
<th data-field="date" data-filter-control="select" data-sortable="true">Date</th>
<th data-field="examen" data-filter-control="select" data-sortable="true">Examen</th>
<th data-field="note" data-sortable="true">Note</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td id="1"https://www.jb51.cc/tag/dis/" target="_blank">display: none;"></td>
<tdhttps://www.jb51.cc/tag/Box/" target="_blank">Box"><input data-index="0" name="btSelectItem" type="checkBox"></td>
<td>Valérie</td>
<td>01/09/2015</td>
<td>Français</td>
<td>12/20</td>
</tr>
</tbody>
</table>
我想在tbody的div上添加表JavaScript,但是他总是追随我的tbody.
问题是,即使我为我的JavaScript创建了另一个div,他也永远不会继续进行此div操作,因此我想将我的JavaScript放在div选项卡上.
例如,我们有一个对象数组
employees: [
{
name: 'John Doe',phone: '00000000',},{
name: 'George Daniels',phone: '11111111',}
]
和表格html对象.
您可以像这样追加行:
for (let employee of employees) {
let row = document.createElement("tr");
row.innerHTML = `<td>${employee.name}</td>
<td>${employee.phone}</td>`;
table.appendChild(row);
}
关于如何用Jsoup填写表格?的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android中用jsoup提取html表格的问题、javascript – 如何在填写表格后永久禁用按钮?、javascript – 如何用JSON填写表单?、javascript-如何用js添加表格?等相关知识的信息别忘了在本站进行查找喔。
本文标签: