Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:48:01+00:00 2026-06-16T11:48:01+00:00

Using Scrapy I’d like to parse a webpage containing a very unsemantic table. What

  • 0

Using Scrapy I’d like to parse a webpage containing a very unsemantic table. What I’m looking for is a “print every following-sibling until you meet the following element”-XPath-query.

<table>
    <tr>
        <th>Title</th>
        <th>Name</th>
        <th>Comment</th>
        <th>Note</th>
    </tr>
    <tr style="background-color:#CCDDEF;">
        <td colspan="4"> <b>HEADER1</b></td>
    </tr>
    <tr>
        <td>Title1.1</td>
        <td>-</td>
        <td>Info1.1</td>
        <td></td>
    </tr>
    <tr style="background-color:#CCDDEF;">
        <td colspan="4"> <b>HEADER2</b></td>
    </tr>
    <tr>
        <td>Title2.1</td>
        <td>Name2.1</td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>Title2.2</td>
        <td>Name2.2</td>
        <td>Info2.2</td>
        <td></td>
    </tr>
    <tr style="background-color:#CCDDEF;">
        <td colspan="4"> <b>HEADER3</b></td>
    </tr>
    <tr>
        <td>Title3.1</td>
        <td>Name3.1</td>
        <td></td>
        <td></td>
    </tr>
</table>

I’d like to group every Title, Name, Comment and Note under each header. I have tried with various XPaths (with variations of following-sibling, preceding-sibling and count) but I either get nothing, everything or every tr which is not a header.

I’m currently getting the headers with //tr[@style] or //tr[td[@colspan="4"]].

The following is the parse-function in my Scrapy-spider (which prints the header and all of the tr‘s which is not a header):

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//*[@id="content-text"]//tr[td[@colspan="4"]]')
    for site in sites:
        print site.select('./td/b/text()').extract()
        print site.select('./following-sibling::tr[not(td[@colspan])]')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:48:02+00:00Added an answer on June 16, 2026 at 11:48 am

    This XPath expression:

    /*/tr[@style or td[@colspan='4']][1]/following-sibling::tr
           [count(. | /*/tr[@style or td[@colspan='4']][2]/preceding-sibling::tr)
           =
            count(/*/tr[@style or td[@colspan='4']][2]/preceding-sibling::tr)
           ]
    

    selects all tr elements that are between the 1st and 2nd headers:

    <tr>
       <td>Title1.1</td>
       <td>-</td>
       <td>Info1.1</td>
       <td/>
    </tr>
    

    To select all tr elements that are between the Kth and (K+1)th headers, simply replace in the above expression 1 with K (the number) and 2 with K+1 (the number).

    XSLT – based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
         "/*/tr[@style or td[@colspan='4']][1]/following-sibling::tr
                 [count(. | /*/tr[@style or td[@colspan='4']][2]/preceding-sibling::tr)
                 =
                  count(/*/tr[@style or td[@colspan='4']][2]/preceding-sibling::tr)
                 ]
         "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <table>
        <tr>
            <th>Title</th>
            <th>Name</th>
            <th>Comment</th>
            <th>Note</th>
        </tr>
        <tr style="background-color:#CCDDEF;">
            <td colspan="4">
                <b>HEADER1</b>
            </td>
        </tr>
        <tr>
            <td>Title1.1</td>
            <td>-</td>
            <td>Info1.1</td>
            <td></td>
        </tr>
        <tr style="background-color:#CCDDEF;">
            <td colspan="4">
                <b>HEADER2</b>
            </td>
        </tr>
        <tr>
            <td>Title2.1</td>
            <td>Name2.1</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>Title2.2</td>
            <td>Name2.2</td>
            <td>Info2.2</td>
            <td></td>
        </tr>
        <tr style="background-color:#CCDDEF;">
            <td colspan="4">
                <b>HEADER3</b>
            </td>
        </tr>
        <tr>
            <td>Title3.1</td>
            <td>Name3.1</td>
            <td></td>
            <td></td>
        </tr>
    </table>
    

    the Xpath expression is evaluated and the selected nodes are copied to the output:

    <tr>
       <td>Title1.1</td>
       <td>-</td>
       <td>Info1.1</td>
       <td/>
    </tr>
    

    Explanation:

    This is a simple application of the Kayessian (after Dr. Michael Kay) formula for node-set intersection:

    $ns1[count(.|$ns2) = count($ns2)]
    

    In this particulat case we substitute $ns1 with:

    /*/tr[@style or td[@colspan='4']][1]/following-sibling::tr
    

    and we substitute $ns2 with:

    /*/tr[@style or td[@colspan='4']][2]/preceding-sibling::tr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse sitemap.xml files using scrapy, the sitemap files are like
I did scraping text from webpage using scrapy. In spider, I have code like:
I'm using Scrapy to crawl a webpage. Some of the information I need only
I am using scrapy to scrap the website. My items are appearing like this
i am using scrapy 0.14 bellow is my code start_urls = ['http://lakebagger.com/ur/'] def parse(self,
I have scraped a webpage using Scrapy and need to extract the background color
I'm new with using scrapy and i'm trying to get some info from a
I am using scrapy to scrap a site I had written a spider and
I'm having a problem iterating a crawl using scrapy. I am extracting a title
Trying to install Scrapy on Mac OSX 10.6 using this guide : When running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.