当我指定不时 Xpath 解析整个页面 [英] Xpath parsing the whole page when i specify not to

查看:22
本文介绍了当我指定不时 Xpath 解析整个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 和 XPath 解析网站.

我想要做的是从

中提取 href

XML(页面)是这样的:

<div align="center"><表格><tr><td><td><a href="test01"><tr><td><tr><td><div align="center"><表格><tr><td><td><a href="test01"><tr><td><tr><td>

这是我做的代码:

posts = page.xpath("//div[@id='posts']/div[@align='center']")在帖子中发布:打印 post.xpath("//table/tr[1]/td[2]/a/@href")

但问题是我最终得到了 posts 的每一个 href 而不是 post

中的一个

我做错了什么?

解决方案

/ 字符开头的 XPath 意味着它将从文档根节点开始.要从上下文节点创建相对 XPath,您需要在 /.

之前放置一个 .

所以你的代码应该是:

posts = page.xpath("//div[@id='posts']/div[@align='center']")在帖子中发布:打印 post.xpath(".//table/tr[1]/td[2]/a/@href")

I'm parsing websites using python and XPath.

What I'm trying to do is to extract the href from the <a>

So here's how is the XML (page):

<div id="post">
  <div align="center">
    <table>
      <tbody>
        <tr>
          <td>
          <td>
            <a href="test01">
        <tr>
          <td>
        <tr>
          <td>
  <div align="center">
    <table>
      <tbody>
        <tr>
          <td>
          <td>
            <a href="test01">
        <tr>
          <td>
        <tr>
          <td>

And here's the code I did:

posts = page.xpath("//div[@id='posts']/div[@align='center']")
for post in posts :
  print post.xpath("//table/tr[1]/td[2]/a/@href")

But the problem is that I end up with every href of posts and not the single one from post

What am I doing wrong ?

解决方案

An XPath starting with a / character means that it will be begin at the document root node. To create a relative XPath from the context node, you need to put a . before the /.

So your code should be:

posts = page.xpath("//div[@id='posts']/div[@align='center']")
for post in posts:
  print post.xpath(".//table/tr[1]/td[2]/a/@href")

这篇关于当我指定不时 Xpath 解析整个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆