Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to parse href tags #1

Open
SandeshSarfare opened this issue Sep 28, 2016 · 4 comments
Open

Unable to parse href tags #1

SandeshSarfare opened this issue Sep 28, 2016 · 4 comments

Comments

@SandeshSarfare
Copy link

I have list tables with below structure and I want to get all href from those tables
Is it possible to accomplish this?

<table class="table-grid" width="100%" border="0" style="margin-top:15px;">
                <tbody>
                <tr>
                    <td colspan="3"><strong>
                            <a href="/abc/7765332" class="abc-7765332">clickable</a> </strong>
                    </td>
                </tr>
</tbody>
</table>
<table class="table-grid" width="100%" border="0" style="margin-top:15px;">
                <tbody>
                <tr>
                    <td colspan="3"><strong>
                            <a href="/xyz/7765332" class="xyz-7765332">clickable</a> </strong>
                    </td>
                </tr>
</tbody>
</table>
@SandeshSarfare
Copy link
Author

var $ = cheerio.load(html);
      cheerioTableparser($);
      var data = $(".qualification-grid").parsetable(false, false, false);
      //var strong = $('<div>' + data[0][0] + '</div>').find('a').text();
      console.log(data[0][0]);

with above code I get output as

<strong>
                            <a href="/xyz/7765332" class="xyz-7765332">clickable</a> 
</strong>

Now I just need to get hrefs.

@misterparser
Copy link
Owner

misterparser commented Oct 3, 2016

I hardly can imagine universal solution for my plugin in this case without using eval. Using eval is bad practice I think. Getting attribute from tag not the only possible way to get data from table.

In yours case I think this code will works OK.

function getHrefsFromArray(arr) {
    var result = []
    for (var i = 0; i < arr.length; i++) {
        var row = arr[i];
        result[i] = []
        for (var j = 0; j < row.length; j++) {
            var href = $('<div>' + row[j] + '</div>').find("a").attr("href")
            if (href != undefined) {
                result[i][j] = href
            } else {
                result[i][j] = "" // or some text for undefined result
            }
        }
    }
    return result
}

var $ = cheerio.load(html);
      cheerioTableparser($);
      var data = $(".qualification-grid").parsetable(false, false, false);
      //var strong = $('<div>' + data[0][0] + '</div>').find('a').text();
      console.log(data[0][0]);

hrefsArray =  getHrefsFromArray(data)

@SandeshSarfare
Copy link
Author

Thanks @misterparser ...I will give it a try and keep you posted

@misterparser
Copy link
Owner

If yours task do not connected with table structure you can just do something like this.

$(".qualification-grid a").each(function(i, r) {
    console.log($(r).attr("href"))
})

My plugin is only for solve problem with colspan and rowspan tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants