我正在尝试在一个刮板中进行逆向工程,以生成一个模型来提取数据。
因此,我知道页面的标题,并希望在HTML码中查找它,然后将XPath或CSS Selector返回到这个位置。
我在我的项目中使用了Scrapy,但是,对于这个逆向工程,我想也许Beautiful Soup 4和lxml解析器的结合也可以帮助我。我只是还没有找到任何关于它的文档。
有谁知道有没有办法做到这一点?
发布于 2019-03-20 07:04:14
如果您实际使用的是lxml,则可以使用getpath()...
from lxml import etree
xml = """
<doc>
<one>
<two>
<test>foo</test>
</two>
<two>
<test>bar</test>
</two>
</one>
</doc>
"""
tree = etree.fromstring(xml)
for match in tree.xpath("//*[contains(text(),'bar')]"):
print(etree.ElementTree(tree).getpath(match))这将打印:
/doc/one/two[2]/testhttps://stackoverflow.com/questions/55249936
复制相似问题