简体   繁体   English

如何使用 R 从该网站(多个网页)中抓取数据?

[英]How can I scrape data from this website (multiple webpages) using R?

I am a beginner in scraping data from website.我是从网站上抓取数据的初学者。 It seems difficult for me to interpret the structure of html using XML or other packages.我似乎很难使用 XML 或其他包来解释 html 的结构。 Can anyone help me to download the data from this website?谁能帮我从这个网站下载数据?

http://wszw.hzs.mofcom.gov.cn/fecp/fem/corp/fem_cert_stat_view_list.jsp http://wszw.hzs.mofcom.gov.cn/fecp/fem/corp/fem_cert_stat_view_list.jsp

It is about the investment from China.这是关于来自中国的投资。 The character set is in Chinese.字符集是中文的。

What I've tried so far:到目前为止我尝试过的:

library("rvest")

url <- "http://wszw.hzs.mofcom.gov.cn/fecp/fem/corp/fem_cert_stat_view_list.jsp" 

firm <- url %>% 
  html() %>% 
  html_nodes(xpath='//*[@id="Grid1MainLayer"]/table[1]') %>% 
  html_table() 

firm <- firm[[1]] head(firm)

You can try with the function in the XML package called readHTMLTable that should download all the tables in the page and already format it into a data.frame.您可以尝试使用名为 readHTMLTable 的 XML 包中的函数,该函数应该下载页面中的所有表格并将其格式化为 data.frame。

library(XML)
all_tables = readHTMLTable("http://wszw.hzs.mofcom.gov.cn/fecp/fem/corp/fem_cert_stat_view_list.jsp")

Then since there is only one table in the page you linked it should be enough to get the first element so:然后,由于您链接的页面中只有一个表格,因此应该足以获取第一个元素,因此:

target_table = all_tables[[1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM