简体   繁体   中英

Python: What's wrong with my xpath expression?

I have this:

<div class="down_butt_pad1" style="" id="downloadlink"><a href="http://www.link.com" class="down_butt1" onclick="javascript:window.open('http://s.spam.com','popunder','width=800,height=800,scrollbars=yes,status=no,resizable=yes, toolbar=no'); window.focus();"></a></div>

I'm trying to do a xpath("//div[@id == 'downloadlink']") but I get an error ("lxml.etree.XPathEvalError: Invalid expression").

What's wrong with what I am doing and how can I search for a div with the id that has downloadlink instead then?

You have to use a single = in XPath. What you're looking for is

"//div[@id='downloadlink']"

However , there is nothing interesting about the div. Why stop there? Perhaps you want to extract the URL:

"//div[@id='downloadlink']/a/@href"

I recommend using a deep dive to extract exactly what you want, rather than stopping at a container element, then invoking the xpath engine again or switching to tree-style navigation.

I should be

"//div[@id='downloadlink']"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM