【问题标题】:Parsing adjacent items in Scrapy在 Scrapy 中解析相邻项目
【发布时间】:2013-09-26 12:32:33
【问题描述】:

对 Scrapy 和 Python 还是有点陌生​​,所以希望得到一些指导。现在我正在尝试使用默认的 lxml 解决这个问题,但我听说 BeautifulSoup 也很好。如果其他选择器使这更容易,可以尝试其他选择器。

我正在尝试抓取一个结构非常糟糕的 html 对象。对象的详细信息没有嵌套在与其名称相同的结构中(没有 div 或任何东西)。我不知道如何提取节点,然后从相邻节点中提取细节(而不是从嵌套元素中)

它目前是这样定义的:

<!--first item-->
<h2 class="name">
    <a href="link">Name goes here</a>
</h2>
<table>
<tbody>
    <tr>
        <th>ID</th>
        <td><a href="link">123456</a></td>
    </tr>
    <tr>
        <th>Description</th>
        <td>More details here</td>
    </tr>
    <tr>
        <th>Date</th>
        <td>1/1/13</td>
    </tr>
</tbody>
</table>
<!-- end first item -->

<hr>

<!--second item-->
<h2 class="name">
   <a href="link">Name goes here</a>
</h2>
<table>
<tbody>
    <tr>
        <th>ID</th>
        <td><a href="link">123456</a></td>
....

请注意 ID、描述和日期如何不嵌套在与名称相同的 div 中。另外,第二项与第一项的名称和详细信息相邻。

对此的任何帮助表示赞赏。

【问题讨论】:

    标签: python beautifulsoup screen-scraping scrapy


    【解决方案1】:

    你想要following-sibling。如:

    hxs.select("//th[text()='ID']/following-sibling::td[1]/a/@href")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2021-01-27
      相关资源
      最近更新 更多