问题
I am trying to extract xpath from html using xidel and recently encountered invalid html
i use firefox to get the xpath, but firefox automatically adds missing tags so the xpath doesent match
can i stop firefox, or can you suggest a way to deal with this?
also is there someway of a reverse xpath? to get the xpath of some text?
回答1:
As Jim Garrison already mentioned: XPath is not going to work if your html is invalid. To answer to your second question: You can get the XPath of some text easily using an browser extention/addon.
For Firefox you can for instance use XPath Checker.
I use XPath Helper on Chrome.
Just keep in mind that the results may be wrong when you are trying to get the XPath of a not well-formed html document.
回答2:
If the XML/HTML is invalid, Xidel will repair it, before applying the XPath.
Although it might repair it differently than Firefox. You can see how it was changed with:
xidel http://yourwebpage -e / --html
If you save that output and open it in Firefox, you can make the XPath for that.
Generally, the repairing might change intermediate tags, but it will probably keep classes and ids unchanged. You can thus replace some XPath like /html/body/div[2]/div[@id="foo"]/p[1]/p/text()
with //div[@id="foo"]/p[1]/span/text()
or //div[@id="foo"]//span[1]/text()
来源:https://stackoverflow.com/questions/32978944/how-to-get-xpath-of-invalid-html