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

  • SEARCH
  • Home
  • 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 6114945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:05+00:00 2026-05-23T15:02:05+00:00

I´m all new to scraping and I´m trying to understand xpath using R. My

  • 0

I´m all new to scraping and I´m trying to understand xpath using R. My objective is to create a vector of people from this website. I´m able to do it using :

r<-htmlTreeParse(e) ## e is after getURL 
    g.k<-(r[[3]][[1]][[2]][[3]][[2]][[2]][[2]][[1]][[4]])
    l<-g.k[names(g.k)=="text"]
    u<-ldply(l,function(x) {

        w<-xmlValue(x)
        return(w)
        })

However this is cumbersome and I´d prefer to use xpath. How do I go about referencing the path detailed above? Is there a function for this or can I submit my path somehow referenced as above?

I´ve come to

xpathApply( htmlTreeParse(e, useInt=T), "//body//text//div//div//p//text()", function(k) xmlValue(k))->kk

But this leaves me a lot of cleaning up to do and I assume it can be done better.

Regards,
//M

EDIT: Sorry for the unclearliness, but I´m all new to this and rather confused. The XML document is too large to be pasted unfortunately. I guess my question is whether there is some easy way to find the name of these nodes/structure of the document, besides using view source ? I´ve come a little closer to what I´d like:

getNodeSet(htmlTreeParse(e, useInt=T), "//p")[[5]]->e2

gives me the list of what I want. However still in xml with br tags. I thought running

xpathApply(e2, "//text()", function(k) xmlValue(k))->kk

would provide a list that later could be unlisted. however it provides a list with more garbage than e2 displays.

Is there a way to do this directly:

xpathApply(htmlTreeParse(e, useInt=T), "//p[5]//text()", function(k) xmlValue(k))->kk

Link to the web page: I´m trying to get the names, and only, the names from the page.

getURL("http://legeforeningen.no/id/1712")
  • 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-05-23T15:02:05+00:00Added an answer on May 23, 2026 at 3:02 pm

    I ended up with

    xml = htmlTreeParse("http://legeforeningen.no/id/1712", useInternalNodes=TRUE)
    

    (no need for RCurl) and then

    sub(",.*$", "", unlist(xpathApply(xml, "//p[4]/text()", xmlValue)))
    

    (subset in xpath) which leaves a final line that is not a name. One could do the text processing in XML, too, but then one would iterate at the R level.

    n <- xpathApply(xml, "count(//p[4]/text())") - 1L
    sapply(seq_len(n), function(i) {
        xpathApply(xml, sprintf('substring-before(//p[4]/text()[%d], ",")', i))
    })
    

    Unfortunately, this does not pick up names that do not contain a comma.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this example to fetch links from a website : http://www.merchantos.com/makebeta/php/scraping-links-with-php/ $xpath =
I am all very new to this and am trying to use extended hex
I'm all new to Android and I'm trying to create a spinner programmatically and
What I want: Update all new commits from server with my local repository in
I am currently trying to learn all new features of C#3.0. I have found
We are trying to get the Model-View-Presenter pattern used on (virtually) all new dev
I've been using ShowDialog() in following way for all my new Windows. SomeGui test
I am trying to make a firefox extension which looks at all the new
I'm stuck on this and have been all day.. I'm still pretty new to
This is probably one of the things that all new users find out about

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.