Jsoup POST:定义选定的选项以返回HTML? [英] Jsoup POST: Defining a selected option to return HTML?

查看:181
本文介绍了Jsoup POST:定义选定的选项以返回HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为包含选项列表的页面生成HTML帖子。有一个下拉列表,用户选择一个项目,然后单击生成按钮。根据所选的选项,它会返回各种结果。我对之前和之后的H​​TML做了比较,可以看到如下差异:

I'm trying to generate a HTML post to a page which has a list of options. There is a drop down list where the user selects an item and then clicks a generate button. Depending on the option selected it returns various results. I did a comparison of the HTML before and after and can see the differences as follows:

之前

<option value="Option1">Option 1</option>
<option value="Option2">Option 2</option>
<option value="Option3">Option 3</option>

之后

<option value="Option1">Option 1</option>
<option selected="selected" value="Option2">Option 2</option>
<option value="Option3">Option 3</option>

我无法弄清楚jsoup文档生成的语法。到目前为止,我有这个,但它只是不断返回原始HTML而没有结果:

I can't figure out the syntax for the jsoup document generation. So far I have this but it just keeps returning the original HTML with no result:

doc = Jsoup.connect("MYurl...")
.timeout(5000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.cookie("auth", "token")
.data("selected", "Option2")
.post();


推荐答案

您必须使用数据来设置字段的值名称属性

You have to use data to set a field's value by its name attribute.

使用时:

.data("selected", "Option2")

与请求页面上的此元素相同表单(注意 name =selected):

Is the same as having this element on the requesting page form (notice the name="selected"):

<select name="selected">
    <option value="Option1">Option 1</option>
    <option selected="selected" value="Option2">Option 2</option>
    <option value="Option3">Option 3</option>
</select>

所以,你试图 POST的方式是对的。事情可能是名为的参数(字段)选择表单中不存在(您试图模仿) )因此发送它与发送任何内容相同。

So, the way you are trying to POST is correct. The thing is probably the parameter (field) named selected does not exist in the form (that you are trying to emulate) and thus sending it is the same as sending nothing.


我真正需要做的是在解析前点击网页元素,据我所知,这不能用Jsoup完成。

What I really need to do perform a click on the web page element before parsing and from what I understand this can't be done with Jsoup.

你是对的。 Jsoup不支持 - 点击可能有几个副作用,如JavaScript事件等。处理它们是一件大事。

You are correct. Jsoup doesn't support that - a click can have several side-effects like JavaScript events and so on. Processing them is a big deal.

但是,对于你的特定情况,jsoup改变DOM的能力可能很有用。请查看以下示例。在其中我们通过显式设置选择的属性来选择选择来选择一个选项(并从其他所有选项中删除它) )。

To your specific case, though, jsoup's capabilities of altering the DOM can be useful. Check the example below. In it we "select" an option by explicitly setting its selected attribute as selected (and remove it from every other option).

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

public class JSoupChangeDom {
    public static void main(String[] args) {
        Document doc = Jsoup.parse(""+
        " <html><body>                                            " +
        " <div>example</div>                                      " +
        " <form>                                                  " +
        "    <select name='mySelect'>                             " +
        "       <option value='Option1'>Option 1</option>         " +
        "       <option value='Option2'>Option 2</option>         " +
        "       <option value='Option3'>Option 3</option>         " +
        "    </select>                                            " +
        " <form>                                                  " +
        " </body></html>                                          ");
        Element mySelect = 
                      doc.getElementsByAttributeValue("name", "mySelect").get(0);
        String optionValueToBeSelected = "Option2";
        Elements options = mySelect.getElementsByTag("option");
        for (Element option : options) {
            if (option.attr("value").equals(optionValueToBeSelected)) {
                option.attr("selected", "selected");
            } else {
                option.removeAttr("selected");
            }
        }
        System.out.println(doc);
    }
}

输出:

<html>
    <head></head>
    <body>
        <div>example</div>
        <form>
            <select name="mySelect">
                <option value="Option1">Option 1</option>
                <option value="Option2" selected="selected">Option 2</option>
                <option value="Option3">Option 3</option>
            </select>
        </form>
    </body>
</html>

这篇关于Jsoup POST:定义选定的选项以返回HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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