如何检查 Scrapy 中是否存在特定按钮? [英] How to check if a specific button exists in Scrapy?

查看:47
本文介绍了如何检查 Scrapy 中是否存在特定按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页中有一个按钮

 <input class="nextbutton" type="submit" name="B1" value="Next 20>>"></input> 

现在我想检查页面上是否存在此按钮或不使用 Xpath 选择器,以便如果存在,我可以转到下一页并从那里检索信息.

Now i want to check if this button exists on the page or not using Xpath selectors so that if it exists i can go to next page and retreive information from there.

推荐答案

首先,您必须确定什么算作这个按钮".鉴于上下文,我建议寻找带有nextbutton"类的输入.您可以在 XPath 中检查只有一个类的元素:

First, you have to determine what counts as "this button". Given the context, I'd suggest looking for an input with a class of 'nextbutton'. You could check for an element with only one class like this in XPath:

//input[@class='nextbutton']

但这仅查找完全匹配.所以你可以试试:

But that looks for exact matches only. So you could try:

//input[contains(@class, 'nextbutton')]

虽然这也将匹配nonextbutton"或nextbuttonbig".所以你的最终答案可能是:

Though this will also match "nonextbutton" or "nextbuttonbig". So your final answer will probably be:

//input[contains(concat(' ', @class, ' '), ' nextbutton ')]

在 Scrapy 中,一个选择器将评估为如果匹配某些非零内容,则为 true.所以你应该能够写出类似的东西:

In Scrapy, a Selector will evaluate as true if it matches some nonzero content. So you should be able to write something like:

from scrapy.selector import Selector
input_tag = Selector(text=html_content).xpath("//input[contains(concat(' ', @class, ' '), ' nextbutton ')]")
if input_tag: 
    print "Yes, I found a 'next' button on the page."

这篇关于如何检查 Scrapy 中是否存在特定按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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