Comments (6)
Sorry, I can't change that. I use lxml for a years and this behaviour seems to me natural. That is how lxml works:
>>> from lxml.html import fromstring
>>> HTML = "<body><table><tr><td>1</td><td>2</td></tr></table></body"
>>> elem = fromstring(HTML).xpath('//td')[0]
>>> elem.text
'1'
>>> elem.xpath('//td')
[<Element td at 0x1b89b30>, <Element td at 0x1b89bf0>]
You can see that elem.xpath('//td') searches elements from the root of the document and not from the current elemen.
If you want to search from the current element use: .//
from grab.
It's easier than you think :), just make your element the tree element:
In [1]: from lxml.html import fromstring
In [2]: HTML = "<body><table><tr><td>1</td><td>2</td></tr></table></body"
In [3]: elem = fromstring(HTML).xpath('//td')[0]
In [4]: elem.text
Out[4]: '1'
In [5]: elem.xpath('//td')
Out[5]: [<Element td at 0x994c11c>, <Element td at 0x994c38c>]
In [9]: import lxml
In [11]: root = lxml.etree.ElementTree(elem)
In [12]: root.xpath('//td')
Out[12]: [<Element td at 0x994c11c>]
from grab.
It was not an example of why that could not be done. It was an example of that when you get some Element from results of calling lxml xpath
method the "//" queries on that elements are addressed from the root of the tree.
Besides that, there is really bad drawback (at least, for me) in making element the root of tree. In this case you loose the connection to other parts of the tree
>>> root.xpath('..')
[]
from grab.
Ok, I understand your point of view. But, nevertheless, I think, that when somebody iterates through the selected elements he/she doesn't interesting in returning back to parent node. Like here:
for item in grab.doc.select('//div[@class="item"]'):
# i interested only in item's content
If I want to get parent node I would just create the variable of parent node, and then iterate through it. But of course this is just my programming style.
from grab.
I can't find right now examples of source code but I remember I used multiple times addressing to parent or sibling elements via xpath expressions. BTW, do you know how scrapy selectors work? Do they use Grab approach or your approach?
from grab.
Unfortunately I never work with scrapy :)
from grab.
Related Issues (20)
- Remove multicurl transport HOT 1
- empty request headers and body in *.log files when using log_dir option
- spider: impossible to setup grab transport HOT 2
- Can i use grab with async? HOT 1
- Segmentation fault 11 HOT 2
- import error in windows HOT 2
- Typo in documentation HOT 1
- Support ubuntu 19.04 / python3.7 and python3.8 HOT 3
- How to filter duplicate urls HOT 2
- Deprecation warning for defusedxml.lxml HOT 1
- Fix simple typo: shuld -> should
- In Windows XP HOT 9
- Не устанавливается в Линукс HOT 7
- похоже метод в Thread isAlive() был переименован в is_alive() в Python 3.9.0 HOT 2
- On the github page the link to http://grablib.org/ seems broken. HOT 1
- Why not have any new releases on PyPi ? HOT 4
- Failed pycurl/resolve/cookies test
- Fix tests for python 3.10: SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats HOT 2
- Wrong Thread method for Python 3.9.0+ HOT 1
- Fix grammar/spelling errors in README.md
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grab.