Answer the question
In order to leave comments, you need to log in
How to exclude a certain node from processing in grab for python?
There is this code:
from grab import Grab
g = Grab()
g.go('http://habrahabr.ru/post/241889/')
xpath = '//div[contains(@class, "content_left")]//div[contains(@class, "content")]'
print(g.doc.select(xpath).html())
//div[contains(@class, "polling")]
,? //div | //span
only the first one is processed.
Answer the question
In order to leave comments, you need to log in
1. Solution:
from grab import Grab
from grab.tools.lxml_tools import drop_node
url = 'http://habrahabr.ru/post/241889/'
xpath = '//div[contains(@class, "content_left")]//div[contains(@class, "content")]'
drop = '//div[contains(@class, "polling")]'
g = Grab()
g.go(url)
page = g.doc.select(xpath)
drop_node(page.node(), drop)
for element in page:
print(element.html())
grab.doc.select()
returns an iterable object. Here is the solution:from grab import Grab
g = Grab()
g.go(url)
xpath = '//div | //span'
for element in g.doc.select(xpath):
print(element.html())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question