gpt4 book ai didi

python - 如何从 XPath 查询中的先前属性值中提取嵌入的属性值?

转载 作者:可可西里 更新时间:2023-11-01 14:56:49 24 4
gpt4 key购买 nike

我正在尝试从以下 html 部分的 onclick 属性中“选择”链接

<span onclick="Javascript:document.quickFindForm.action='/blah_blah'" 
class="specialLinkType"><img src="blah"></span>

但不能比下面的 XPath 更进一步

//span[@class="specialLinkType"]/@onclick

只返回

Javascript:document.quickFindForm.action

关于如何使用 XPath 在 quickFindForm.action 中选择该链接有什么想法吗?

最佳答案

我在 Java 应用程序中尝试了 XPath,它工作正常:

    import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Teste {

public static void main(String[] args) throws Exception {
Document doc = stringToDom("<span onclick=\"Javascript:document.quickFindForm.action='/blah_blah'\" class=\"specialLinkType\"><img src=\"blah\"/></span>");
XPath newXPath = XPathFactory.newInstance().newXPath();
XPathExpression xpathExpr = newXPath.compile("//span[@class=\"specialLinkType\"]/@onclick");
String result = xpathExpr.evaluate(doc);
System.out.println(result);

}

public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
}

结果:

Javascript:document.quickFindForm.action='/blah_blah'

关于python - 如何从 XPath 查询中的先前属性值中提取嵌入的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com