我试图从一个网页收集信息,但找不到正确的XPath。以下是一篇来自网站的文章:
<div class="posted">
<div>
June 20, 2018
</div>
</div>我想搜索每一页上写着"posted“的除法类,然后以字符串的形式返回它下面的所有内容。(乱七八糟的字符串是可以的;我只会用“possibleDate”中的"if“"2018”来搜索年份)下面是我正在尝试的:
possibleDate = str(tree.xpath("//div[contains(@class, ’posted’)]//@text"))它说它是一个无效的表达式。
我做错了什么?
发布于 2018-06-26 18:44:47
首先,将’字符替换为围绕posted的'字符。
接下来,将@text替换为text(),以消除XPath语法错误。
此外,您可能希望使用所选div的空间规范化字符串值,而不是选择文本节点:
possibleDate = str(tree.xpath("normalize-space(//div[@class='posted'])")这将抽象出嵌套在目标div中的标记变体。
也请参阅: xpath: find a node whose class attribute matches a value and whose text contains a certain string
https://stackoverflow.com/questions/51049546
复制相似问题