Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing URL strings in R

Tags:

r

Let's say I have a series of URL strings that I've imported into R.

url = c("http://www.mdd.com/food/pizza/index.html", "http://www.mdd.com/build-your-own/index.html",
        "http://www.mdd.com/special-deals.html", "http://www.mdd.com/find-a-location.html")

I want to parse through these url's to identify what page they are. I want to be able to map url[3] to special deals page. For this example, let's say I have the following 'types' of pages.

xtype = c("deals","find")
dtype = c("ingrediants","calories","chef")

Given these types, I want to take the url variable and map them together.

So I should end up with:

> df
                                           url  site
1     http://www.mdd.com/food/pizza/index.html dtype
2 http://www.mdd.com/build-your-own/index.html dtype
3        http://www.mdd.com/special-deals.html xtype
4      http://www.mdd.com/find-a-location.html xtype

I began looking into this project by thinking that I'd need to use strsplit to strip apart each url. However, the following doesn't work to split apart the url. Splitting apart the url's would allow me to put together some if-else statements for performing this task. Efficient? No, but as long as it get's the job done.

Words = strsplit(as.character(url), " ")[[1]]
Words

Here are my main questions:

1. Is there a package to do URL parsing in R?
2. How can I go about identifying the page which is viewed from a large url string?

EDIT:

What I'm asking is this: How can I figure out the 'specific page' from a url string. So if I have "http://www.mdd.com/build-your-own/index.html" I want to know how I can extract just build-your-own.

like image 534
amathew Avatar asked Dec 16 '22 01:12

amathew


1 Answers

There's also the urltools package now which is infinitely faster than most other methods:

url <- c("http://www.mdd.com/food/pizza/index.html", 
         "http://www.mdd.com/build-your-own/index.html",
         "http://www.mdd.com/special-deals.html", 
         "http://www.mdd.com/find-a-location.html")

urltools::url_parse(url)

##   scheme      domain port                      path parameter fragment
## 1   http www.mdd.com          food/pizza/index.html                   
## 2   http www.mdd.com      build-your-own/index.html                   
## 3   http www.mdd.com             special-deals.html                   
## 4   http www.mdd.com           find-a-location.html                   
like image 190
hrbrmstr Avatar answered Jan 07 '23 04:01

hrbrmstr