2015-10-16 45 views
0

我想刮掉并使用R.提取所有相关链接的列表。 例如,考虑:List of Cuisines on wikipedia这里的美食分为区域,种族等,这些链接本身和更进一步细分为更多的链接和层次结构。我想提取R. 这整个层次使用正则表达式的一般定义链接将在返回网页上的所有链接,但我想有,所有的依赖关系列出一个表,如:使用R的链接的链接清单

  1. 名单美食:
    • 目录亚洲美食
    • 列表欧洲美食的中欧美食的
    • 列表
    • 奥地利美食
    • 保加利亚美食
    • 捷克美食
    • 德国菜......等等。
    • 列表海洋美食 的...

我知道怎么凑数据从使用R.我是相当新的,并想知道我如何去提取依赖一个网页链接之间。

+0

你想要的是真的不CL耳。上述“正则表达式”和所需输出_in'dput()'风格的R objects_的一个例子可以帮助人们帮助你。你也没有提供任何代码来表明你真的知道如何去做这些刮擦(这不是代码写入服务)。 – hrbrmstr

回答

1

你可以做,例如下面的:如果这是你正在寻找

require(rvest) 
require(magrittr) 
session <- html_session("https://en.wikipedia.org/wiki/List_of_cuisines") 
session %>% html_nodes("ul:nth-child(13) a") %>% html_text() 
[1] "Ainu"    "Akan"    "Arab"    "Assyrian"   "Balochi"   
[6] "Berber"    "Buddhist"   "Bulgarian"   "Cajun"    "Chinese Islamic" 
[11] "Circassian"   "Crimean Tatar"  "Inuit"    "Italian American" "Jewish"    
[16] "Sephardic"   "Mizrahi"   "Bukharan"   "Syrian Jewish"  "Kurdish"   
[21] "Malayali Food"  "Louisiana Creole" "Maharashtrian"  "Mordovian"   "Native American" 
[26] "Parsi"    "Pashtun"   "Pennsylvania Dutch" "Peranakan"   "Persian cuisine" 
[31] "Punjabi"   "Rajasthani"   "Romani"    "Sami"    "Sindhi"    
[36] "Tatar"    "Yamal"    "Zanzibari"   "South Indian"  

什么?如果您想更深入和刮的所有环节,你可以去上进行如下:

cousin_links <- session %>% html_nodes("ul:nth-child(13) a") %>% html_attr("href") 
articles <- lapply(cousin_links, jump_to, x = session) 
explainaition <- lapply(articles, function(a){ 
    a %>% html %>% html_node("p") %>% html_text 
}) 

,让你与第一维基百科释(在一个以上的列表内容盒

> head(explainaition) 
[[1]] 
[1] "Ainu cuisine is the cuisine of the ethnic Ainu in Japan. The cuisine differs markedly from that of the majority Yamato people of Japan. Raw meat like sashimi, for example, is not served in Ainu cuisine, which instead uses methods such as boiling, roasting and curing to prepare meat. The island of Hokkaidō in northern Japan is where most Ainu live today; however, they once inhabited most of the Kuril islands, the southern half of Sakhalin island, and parts of northern Honshū Island." 

[[2]] 
[1] "Akan cuisine, the cuisine of the Akan people, includes meat and fish (seafood) grilled over hot coals, wide and varied range of soups, stews, several kinds of starch foods, groundnut, palm, patties (or empanadas), ground corn (maize), sadza, ugali." 

[[3]] 
[1] "Arab cuisine (Arabic: مطبخ عربي‎) is defined as the various regional cuisines spanning the Arab world, from Mesopotamia to North-Africa. Arab cuisine often incorporates the Levantine and Egyptian culinary traditions." 

[[4]] 
[1] "The cuisine of the indigenous Assyrian people from northern Iraq, north eastern Syria, north western Iran and south eastern Turkey is similar to other Middle Eastern cuisines. It is rich in grains, meat, tomato, and potato. Rice is usually served with every meal accompanied by a stew which is typically poured over the rice. Tea is typically consumed at all times of the day with or without meals, alone or as a social drink. Cheese, crackers, biscuits, baklawa, or other snacks are often served alongside the tea as appetizers. Dietary restrictions may apply during Lent in which certain types of foods may not be consumed; often meaning animal-derived. Alcohol is rather popular specifically in the form of Arak and Wheat Beer. Unlike in Jewish cuisine and Islamic cuisines in the region, pork is allowed, but it is not widely consumed because of restrictions upon availability imposed by the Muslim majority." 

[[5]] 
[1] "Balochi cuisine refers to the food and cuisine of the Baloch people from the Balochistan region, comprising the Pakistani Balochistan province as well as Sistan and Baluchestan in Iran and Balochistan, Afghanistan. Baloch food has a regional variance in contrast to many other cuisines of Pakistan[1][2][3][4] and Iran." 

[[6]] 
[1] "The Amazigh (Berber) cuisine is considered as a traditional cuisine which evolved little in the course of time." 
+0

嗨@FlooO这不正是我一直在寻找我想从维基百科link..in精华提取层次树会像 类型美食 1.美国 - 北美 *加拿大 *加州..等 - 中美洲 * Mexican..etc - 南美 *巴西 * Chilean..etc 2.亚洲 - 东亚 *日本 * Korean..etc 等.. 非常感谢。 –

+0

在哪个网站上您可以找到您想要搜索的信息? – Rentrop

+0

我正在寻找从[链接](http://www.bbc.co.uk/food/ingredients)的所有食品成分清单,并列在“A”下的一个单独的列,在“ B“在一个单独的列中,等等。 –