2014-11-22 78 views
0

提取信息这是我的代码:Scrapy:从特定的div

def parse(self, response): 
    return scrapy.FormRequest.from_response(
     response, 
     formdata={'uuid': 'user', 'password': 'cons'}, 
     callback=self.after_login 
    ) 

def after_login(self, response): 
    # check login succeed before going on 
    if "authentication failed" in response.body: 
     self.log("Login failed", level=log.ERROR) 
else: 
    self.log('LOGGED') 
    sel = Selector(response) 
    sel.xpath("//div[@class='amount cSpringGreen']/text()").extract() 

但是,当我执行它没有出现。它应该工作的方式是在网站上显示该信息后登录。 html代码是这样的。

<h1 class="hide2"></h1> 
<div id="vodaint-local" class="wrapper rhomb"> 
<div class="spring"> 
<script type="text/javascript"> 
<div class="mod mod-selectsizeheader vodaint-local"> 
<div id="mivf" class="content"> 
<div id="navigation-breadcrumb" class="belt"> 
<div class="belt"> 
<div class="miVFR"> 
<div class="mainMiVF cf"> 
<div class="headerMiVF cf"> 
<div class="bodyMiVF cf"> 
<div class="mainNav" style="height: auto;"> 
<div class="mainContent withHeader" style="height: 585px;"> 
<style> 
<div id="contentSpinner" style="margin-bottom: 432px; display: none;"> 
<script> 
<section> 
<script type="text/javascript"> 
<div class="mainContentContainer home"> 
<div class="headerBanner"> 
<script type="text/javascript"> 
<div class="lineContainer "> 
<h6 class="topHeading prepago"> </h6> 
<div class="columnGroup cf"> 
<div class="column newPromo"> 
<div class="columnContent"> 
<p class="cTitle"> Tu saldo</p> 
--THIS IS THE INFO I WANT TO SHOW-- 
<div class="amount cSpringGreen"> 
0, 
<span> 96</span> 
€ 
</div> 

谢谢!

编辑:在这个pastebin中你可以找到整个HTML文件http://pastebin.com/B2HpACCw我想登录后显示的东西是“0'96”,谢谢!

+1

的HTML是有点怪异。有很多没有关闭的'div'和'script'。甚至还有一个可疑的'style'元素。 – dreyescat 2014-11-22 19:15:18

+0

我不确定我是否明白确切的问题 - 是否有任何“'after_login”方法中的消息被打印?究竟是什么问题:蜘蛛没有在网站上登录,或者是数据没有被抓取? – elias 2014-11-22 19:29:37

+0

登录完美地工作,它显示LOGGED在屏幕上,问题是之后,它不显示任何东西。 – AngelaBR 2014-11-23 20:18:20

回答

0

将其存储到项目编辑; items.py

class TestItem(scrapy.Item): 
    text= scrapy.Field() 

,然后在蜘蛛作为

item=TestItem() 
item['text'] = sel.xpath("//div[@class='amount cSpringGreen']/text()").extract() 
print item['text'] 
+0

我不得不修改desc = scrapy.Field()用于文本。 = scrapy.Field()。之后它会提示。 2014-11-26 12:35:48 + 0100 [login] DEBUG:LOGGED []。该项目是空的。 – AngelaBR 2014-11-26 11:37:36

+0

对不起,我只是忘了更新...现在你必须包括项目items.py然后从你的蜘蛛访问它 – Tushar 2014-11-26 11:39:59

+0

是的,我做到了,但它显示为空:/ – AngelaBR 2014-11-26 11:43:49