1

我在网页中有一个按钮

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

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

4

3 回答 3

4

首先,您必须确定什么是“此按钮”。鉴于上下文,我建议寻找具有“nextbutton”类的输入。您可以在 XPath 中检查只有一个类的元素:

//input[@class='nextbutton']

但这仅寻找完全匹配。所以你可以尝试:

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

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

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

在 Scrapy 中,如果 Selector 匹配一些非零内容,它将评估为 true。所以你应该能够写出类似的东西:

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."
于 2014-07-10T09:05:28.113 回答
0

http://www.trumed.org/patients-visitors/find-a-doctor加载iframe一个src="http://verify.tmcmed.org/iDirectory/"

<iframe border="0" frameborder="0" id="I1" name="I1"
        src="http://verify.tmcmed.org/iDirectory/"
        style="width: 920px; height: 600px;" target="I1">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe>

搜索表单位于此 iframe 中。

这是一个说明这一点的scrapy shell会话:

$ scrapy shell "http://www.trumed.org/patients-visitors/find-a-doctor"
2014-07-10 11:31:05+0200 [scrapy] INFO: Scrapy 0.24.2 started (bot: scrapybot)
2014-07-10 11:31:07+0200 [default] DEBUG: Crawled (200) <GET http://www.trumed.org/patients-visitors/find-a-doctor> (referer: None)
...

In [1]: response.xpath('//iframe/@src').extract()
Out[1]: [u'http://verify.tmcmed.org/iDirectory/']

In [2]: fetch('http://verify.tmcmed.org/iDirectory/')
2014-07-10 11:31:34+0200 [default] DEBUG: Redirecting (302) to <GET http://verify.tmcmed.org/iDirectory/applicationspecific/intropage.asp> from <GET http://verify.tmcmed.org/iDirectory/>
2014-07-10 11:31:35+0200 [default] DEBUG: Redirecting (302) to <GET http://verify.tmcmed.org/iDirectory/applicationspecific/search.asp> from <GET http://verify.tmcmed.org/iDirectory/applicationspecific/intropage.asp>
2014-07-10 11:31:36+0200 [default] DEBUG: Crawled (200) <GET http://verify.tmcmed.org/iDirectory/applicationspecific/search.asp> (referer: None)
...

In [3]: from scrapy.http import FormRequest

In [4]: frq = FormRequest.from_response(response, formdata={'LastName': 'c'})

In [5]: fetch(frq)
2014-07-10 11:32:15+0200 [default] DEBUG: Redirecting (302) to <GET http://verify.tmcmed.org/iDirectory/applicationspecific/SearchStart.asp> from <POST http://verify.tmcmed.org/iDirectory/applicationspecific/search.asp>
2014-07-10 11:32:15+0200 [default] DEBUG: Redirecting (302) to <GET http://verify.tmcmed.org/iDirectory/applicationspecific/searchresults.asp> from <GET http://verify.tmcmed.org/iDirectory/applicationspecific/SearchStart.asp>
2014-07-10 11:32:17+0200 [default] DEBUG: Crawled (200) <GET http://verify.tmcmed.org/iDirectory/applicationspecific/searchresults.asp> (referer: None)
...

In [6]: response.css('input.nextbutton')
Out[6]: [<Selector xpath=u"descendant-or-self::input[@class and contains(concat(' ', normalize-space(@class), ' '), ' nextbutton ')]" data=u'<input type="submit" value=" Next 20 &gt'>]

In [7]: response.xpath('//input[@class="nextbutton"]')
Out[7]: [<Selector xpath='//input[@class="nextbutton"]' data=u'<input type="submit" value=" Next 20 &gt'>]

In [8]: 
于 2014-07-10T09:34:52.227 回答
0

由于Scrapy Selectors 文档,您可以使用 xpath 和 element 属性来检查元素是否存在。

尝试这个!

isExists = response.xpath("//input[@class='nextbutton']").extract_first(default='not-found')
if( isExists == 'not-found'):
     # input Not Exists
     pass 
else:
     # input Exists , crawl other page
     pass 
于 2018-09-28T22:17:55.967 回答