如何检查Scrapy中是否存在特定按钮?

如何检查Scrapy中是否存在特定按钮?,scrapy,scrapy-spider,Scrapy,Scrapy Spider,我在网页上有一个按钮 <input class="nextbutton" type="submit" name="B1" value="Next 20>>"></input> 现在,我想检查页面上是否存在此按钮,或者是否使用Xpath选择器,以便如果它存在,我可以转到下一页并从那里检索信息。首先,您必须确定什么是“此按钮”。考虑到上下文,我建议使用“nextbutton”类查找输入。您可以在XPath中检查只有一个类的元素,如下所示: //input

我在网页上有一个按钮

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


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

首先,您必须确定什么是“此按钮”。考虑到上下文,我建议使用“nextbutton”类查找输入。您可以在XPath中检查只有一个类的元素,如下所示:

//input[@class='nextbutton']
但这只是寻找精确的匹配。所以你可以试试:

//input[contains(@class, 'nextbutton')]
虽然这也将匹配“nonextbutton”或“nextbuttonbig”。因此,您的最终答案可能是:

//input[contains(concat(' ', @class, ' '), ' nextbutton ')]
在Scrapy。因此,您应该能够编写如下内容:

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."

首先,您必须确定什么是“此按钮”。考虑到上下文,我建议使用“nextbutton”类查找输入。您可以在XPath中检查只有一个类的元素,如下所示:

//input[@class='nextbutton']
但这只是寻找精确的匹配。所以你可以试试:

//input[contains(@class, 'nextbutton')]
虽然这也将匹配“nonextbutton”或“nextbuttonbig”。因此,您的最终答案可能是:

//input[contains(concat(' ', @class, ' '), ' nextbutton ')]
在Scrapy。因此,您应该能够编写如下内容:

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."
使用
src=”加载
iframe
http://verify.tmcmed.org/iDirectory/“


您的浏览器不支持内嵌框架,或者当前配置为不显示内嵌框架。
搜索表单在此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]: 
$scrapy shell”http://www.trumed.org/patients-visitors/find-a-doctor"
2014-07-10 11:31:05+0200[scrapy]信息:scrapy 0.24.2已启动(机器人:scrapybot)
2014-07-10 11:31:07+0200[默认]调试:爬网(200)(参考:无)
...
[1]中的response.xpath('//iframe/@src').extract()
出[1]:[u'http://verify.tmcmed.org/iDirectory/']
在[2]中:取数http://verify.tmcmed.org/iDirectory/')
2014-07-10 11:31:34+0200[默认]调试:重定向(302)到
2014-07-10 11:31:35+0200[默认]调试:重定向(302)到
2014-07-10 11:31:36+0200[默认]调试:爬网(200)(参考:无)
...
在[3]中:从scrapy.http导入FormRequest
在[4]中:frq=FormRequest.from_response(response,formdata={'LastName':'c'})
在[5]中:获取(frq)
2014-07-10 11:32:15+0200[默认]调试:重定向(302)到
2014-07-10 11:32:15+0200[默认]调试:重定向(302)到
2014-07-10 11:32:17+0200[默认]调试:爬网(200)(参考:无)
...
[6]中的response.css('input.nextbutton')
输出[6]:[]
在[7]:response.xpath('//input[@class=“nextbutton”]')中
Out[7]:[]
在[8]中:
使用
src=”加载
iframe
http://verify.tmcmed.org/iDirectory/“


您的浏览器不支持内嵌框架,或者当前配置为不显示内嵌框架。
搜索表单在此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]: 
$scrapy shell”http://www.trumed.org/patients-visitors/find-a-doctor"
2014-07-10 11:31:05+0200[scrapy]信息:scrapy 0.24.2已启动(机器人:scrapybot)
2014-07-10 11:31:07+0200[默认]调试:爬网(200)(参考:无)
...
[1]中的response.xpath('//iframe/@src').extract()
出[1]:[u'http://verify.tmcmed.org/iDirectory/']
在[2]中:取数http://verify.tmcmed.org/iDirectory/')
2014-07-10 11:31:34+0200[默认]调试:重定向(302)到
2014-07-10 11:31:35+0200[默认]调试:重定向(302)到
2014-07-10 11:31:36+0200[默认]调试:爬网(200)(参考:无)
...
在[3]中:从scrapy.http导入FormRequest
在[4]中:frq=FormRequest.from_response(response,formdata={'LastName':'c'})
在[5]中:获取(frq)
2014-07-10 11:32:15+0200[默认]调试:重定向(302)到
2014-07-10 11:32:15+0200[默认]调试:重定向(302)到
2014-07-10 11:32:17+0200[默认]调试:爬网(200)(参考:无)
...
[6]中的response.css('input.nextbutton')
输出[6]:[]
在[7]:response.xpath('//input[@class=“nextbutton”]')中
Out[7]:[]
在[8]中:
由于存在,您可以使用xpath和元素属性检查元素是否存在

试试这个

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 
由于存在,您可以使用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 

你可以测试一些不同的东西。这个按钮最有区别的特性是什么?是“下一个按钮”课吗?它的名字?它的价值?这些的组合?我尝试使用“hxs.select('//input[@class=“nextbutton”]”),但它返回null。如果可以,请提供示例URL。检查类属性“”中是否没有空格。在提供者姓氏字段中提供“C”,它将向u显示医生名单。由于本例中的列表将很长,因此它显示了“下一个20”按钮,您可以测试一些不同的东西。这个按钮最有区别的特性是什么?是“下一个按钮”课吗?它的名字?它的价值?这些的组合?我尝试使用“hxs.select('//input[@class=“nextbutton”]”),但它返回null。如果可以,请提供示例URL。检查类属性“”中是否没有空格。在提供者姓氏字段中提供“C”,它将向u显示医生名单。由于本例中的列表很长,它显示了XPath的“下一个20”按钮
//input[contains(concat(“”,@class'),'nextbutton')]
是等效的CSS选择器(感谢底层的cssselect)
。CSS(“input.nextbutton”)
是XPath
//input[contains(concat(“”,@class')),'nextbutton')]
是等效的CSS选择器(感谢底层的cssselect)
.CSS(“input.nextbutton”)