Lxml 或 Xpath 内容打印

发布于 2024-08-23 08:21:52 字数 335 浏览 4 评论 0原文

我有以下函数

def parseTitle(self,  post):
    """
        Returns title string with spaces replaced by dots
    ""        
    return post.xpath('h2')[0].text.replace('.',  ' ')

来查看 post 的内容。我已经尝试了我能想到的一切。

如何正确调试内容?这是一个电影网站,我在其中提取链接和标题,并且此函数应该解析标题。

我确信 H@ 不存在,我该如何打印/调试它?

I have the following function

def parseTitle(self,  post):
    """
        Returns title string with spaces replaced by dots
    ""        
    return post.xpath('h2')[0].text.replace('.',  ' ')

I would to see the content of post. I have tried everything I can think of.

How can I properly debug the content? This is an website of movies where I'm rip links and title and this function should parse the title.

I am sure H@ is not existing, how can I print/debug this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-08-30 08:21:52

post 是lxml元素树对象,不是吗?
所以首先,您可以尝试:

# import lxml.html # if not yet imported
# (or you can use lxml.etree instead of lxml.html)
print lxml.html.tostring(post)

如果不是,您应该从中创建元素树对象

post = lxml.html.fromstring(post)

,或者问题可能只是您应该将 h2 替换为 //h2

你的问题不是很解释..

post is lxml element tree object, isn't it?
so first, you could try:

# import lxml.html # if not yet imported
# (or you can use lxml.etree instead of lxml.html)
print lxml.html.tostring(post)

if isn't, you should create element tree object from it

post = lxml.html.fromstring(post)

or maybe the problem is just that you should replace h2 with //h2?

your question is not very explanatory..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文