splash:select()方法与select_all()

select()

该方法可以选中符合条件的第一个节点,如果有多个节点符合条件,则只会返回一个,其参数是CSS选择器。示例如下:

1

2

3

4

5

6

7

function main(splash)

splash:go("https://www.baidu.com/")

input = splash:select("#kw")

input:send_text('Splash')

splash:wait(3)

return splash:png()

end

 

这里我们首先访问了百度,然后选中了搜索框,随后调用了send_text()方法填写了文本,然后返回网页截图。

结果如图7-15所示,可以看到,我们成功填写了输入框。

select_all()

此方法可以选中所有符合条件的节点,其参数是CSS选择器。示例如下:

 

1

2

3

4

5

6

7

8

9

10

11

function main(splash)

local treat = require('treat')

assert(splash:go("http://quotes.toscrape.com/"))

assert(splash:wait(0.5))

local texts = splash:select_all('.quote .text')

local results = {}

for index, text in ipairs(texts) do

results[index] = text.node.innerHTML

end

return treat.as_array(results)

end

 

这里我们通过CSS选择器选中了节点的正文内容,随后遍历了所有节点,将其中的文本获取下来。

运行结果如下:

 

1

2

3

4

5

6

7

8

9

10

11

Splash Response: Array[10]

0: "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”"

1: "“It is our choices, Harry, that show what we truly are, far more than our abilities.”"

2: “There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”

3: "“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”"

4: "“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”"

5: "“Try not to become a man of success. Rather become a man of value.”"

6: "“It is better to be hated for what you are than to be loved for what you are not.”"

7: "“I have not failed. I've just found 10,000 ways that won't work.”"

8: "“A woman is like a tea bag; you never know how strong it is until it's in hot water.”"

9: "“A day without sunshine is like, you know, night.”"

 

可以发现,我们成功地将10个节点的正文内容获取了下来。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
删除之类操作需要全选功能,方便选择 public class MainActivity extends Activity { private ListView lv; private MyAdapter mAdapter; private ArrayList list; private Button bt_selectall; // private Button bt_cancel; // private Button bt_deselectall; private int checkNum; // 记录选中的条目数量 private TextView tv_show;// 用于显示选中的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将MyAdapter中的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的长度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 // bt_cancel.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // // 遍历list的长度,将已选的设为未选,未选的设为已选 // for (int i = 0; i < list.siz

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值