0

This html file

<html>
 <head></head>
 <body>
  <div class="float-r"> 
   <span id="artical_comment_cnt"><i></i>0</span> 
   <span><i class="view"></i><script src="/cms/plus/count.php?view=yes&amp;aid=20192&amp;mid=17" language="javascript"></script>778</span> 
  </div>
 </body>
</html>

I want get "0" and "778" , my Code is

comments = response.css(".float-r span#artical_comment_cnt::text").extract()[0]  
views = response.css("div.float-r span span::text").extract_first().extract()[0]

But result always NULL, how to fix it??

UPDATE

for id artical_comment_cnt is Global only my answer is

In [2]: response.css("span#artical_comment_cnt::text").extract_first()
Out[2]: '0'
1
  • var comments = document.querySelector('.float-r .artical_comment_cnt').innerText Commented Aug 11, 2017 at 11:47

2 Answers 2

3

You have some mistakes in the selectors.

To get the comment, you should write:

>>> response.css(".float-r span#artical_comment_cnt::text").extract_first()
'0'

#artical_comment_cnt instead of .artical_comment_cnt, # is for specify an id, . is for a class.

To get the view, you need to specify,
that you want the second span in div (.float-r) element, nth-child(2) will do it.

>>> response.css("div.float-r span:nth-child(2)::text").extract_first()
'778'

span:nth-child(2) instead of span span

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

comments = response.css(".float-r span#artical_comment_cnt::text").extract_first()
views = response.css("div.float-r span:nth-child(2)::text").extract_first()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.