title: How to scrape data from a website and write to a csv in a specified format in R?
body: <p>I am trying to scrape data from <a href="https://www.booking.com/country.html" rel="nofollow noreferrer">https://www.booking.com/country.html</a>.</p>
  
  <p>The idea is to extract all numbers regarding any kind of accommodation listed for a particular country.</p>
  
  <p>The output needs to have the list of all countries in ‘column A’ of an Excel file and the relevant number of listings for different property types (Ex. Apartments, Hostels, Resorts, etc.) in each respective country adjacent to the country names in separate columns.</p>
  
  <p>I need to capture all the details for all the property types for a given country.</p>
  
  <p><a href="https://i.stack.imgur.com/tJxRE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tJxRE.png" alt="The below image describes the output format required."></a></p>
  
  <p>The above image describes the output format required in Excel. I am able to get the country using the below code but not the property types and their respective data.</p>
  
  <p>How to get the data iteratively in function for all the countries and write in a csv.</p>
  
  <pre><code>library(rvest)
  library(reshape2)
  library(stringr)
  
  url &lt;- "https://www.booking.com/country.html"
  
  bookingdata &lt;- read_html(url)
  
  #extracting the country
  country &lt;- html_nodes(bookingdata, "h2 &gt; a") %&gt;% 
    html_text()
  write.csv(country, 'D:\\web scraping\\country.csv' ,row.names = FALSE)
  print(country)
  
  #extracting the data inside the inner div 
  html_nodes(bookingdata, "div &gt;div &gt; div &gt; ul &gt; li &gt; a")%&gt;%
    html_text()
  for (i in country) {
  print(i)
  html_nodes(pg, "ul &gt; li &gt; a") %&gt;% 
    html_text()
    print(accomodation)
  }
  
  #getting all the data
  accomodation &lt;- html_nodes(pg, "ul &gt; li &gt; a") %&gt;% 
    html_text()
  
  #separating the numbers
  accomodation.num &lt;- (str_extract(accomodation, "[0-9]+"))
  #separating the characters
  accomodation.char &lt;- (str_extract(accomodation,"[aA-zZ]+"))
  #separating unique characters
  unique(accomodation.char)
  </code></pre>