Web Scraping with R: How to Fill Missing Value (爬虫:如何处理缺失值)
发布日期:2021-05-09 09:08:21 浏览次数:18 分类:博客文章

本文共 3776 字,大约阅读时间需要 12 分钟。

������������������������������������������������������������������������������������������������������������

��������� IMDb ��������� ��������������������������������� R ������������������

##### >> Preparation

������ Hadley Wickham ������������������ rvest ������������������������������������������������ rvest ������

#install packageinstall.package('rvest')#loading librarylibrary('rvest')
##### >> Downloading and parsing HTML file

������������������������������ read_html() ��������������������� XML ���������

url <- 'https://www.imdb.com/search/title?count=100&release_date=2018-01-01,2018-12-31&view=advanced'webpage <- read_html(url)
##### >> Extracting Nodes

���������������������������������

  1. Rank: ������
  2. Title���������������
  3. Runtime���������������
  4. Genre���������������
  5. Rating���������������
  6. Metascore���������������
  7. Description���������������
  8. Votes������������������������������
  9. Gross���������������

������ html_nodes() ������������������ XML ������������������������������������������ CSS ������������������������������������

#Using CSS selectors to extract noderank_data_html <- html_nodes(webpage, '.text-primary')#Converting the node to textrank_data <- html_text(rank_data_html)#Converting text value to numeric valuerank_data <- as.numeric(rank_data)

������������������ ��� ������������������������������������ HTML/CSS ��������������������������������� HTML/CSS���������������������������

  1. ������������������ Chrome ��������������������������������������� F12 ���������������������
  2. ���������������������������������������������������������������������������
  3. ���������������������������������������������������������������
  4. ������ CSS ������������������������������������������������

������������������ Script ������������������������������

##### >> Handling Missing Values

������������������������������������������������������������������������������������������������������������������������ Metascore

metascore_data_html <- html_nodes(webpage,'.metascore')metascore_data <- html_text(metascore_data_html)length(metascore_data)

������������������������������������������ NA ������������

��������������������� html_node ��� html_nodes ��������������������� ?html_node ���������������������

html_node is like [[ it always extracts exactly one element. When given a list of nodes, html_node will always return a list of the same length, the length of html_nodes might be longer or shorter.

������������������������������������������������������������������������������ DOM������������������ DOM ������������������������������ DOM���

���������������������������

metascore_data_html <- html_node(html_nodes(webpage, '.lister-item-content'), '.metascore')metascore_data <- html_text(metascore_data_html)length(metascore_data)
##### >> Making a Data Frame

��������������������������������������������������������� data frame ���������������������������

movies <- data.frame(  rank = rank_data,  title = title_data,  description = description_data,  runtime = runtime_data,  genre = genre_data,  rating = rating_data,  metascorre = metascore_data,  votes = votes_data,  gross = gross_data)
##### >> Exporting CSV File

������������������������������������������������������ csv ������������������

write.csv(movies, file = file.choose(new = TRUE), row.names = FALSE)

������������������������������������������������������������������������������

##### >> Notes

rvest ������������������������������������������������������

  1. html_tag(): ������DOM ��� tag name
  2. html_attr(): ������DOM ��� ������������
  3. html_attrs(): ������DOM ��� ������������
  4. guess_encoding() and repair_encoding()��� ��������������������������� ���������������������������������������������~���
  5. jump_to(), follow_link(), back(), forward(): ���������������������������������������������
##### >> Sample Code

上一篇:Survival on the Titanic (泰坦尼克号生存预测)
下一篇:How to Compare Means (均值比较)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月06日 11时11分38秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

VS中 fatal error LNK1123: 转换到 COFF 期间失败 的解决方法 2021-05-12
关于Img标签在固定宽高的容器内部以图片比例缩放存在 2021-05-12
python3---读写文件、POST表单请求、HTML处理、列表分割遍历,进行暴力破解用户名与密码 2021-05-12
Android Studio基础项目-布局XML设置的实战-全屏显示登录界面,去除按钮深颜色 2021-05-12
pyhton---异常处理的终极语法、网页访问基本读取、网页访问异常处理 2021-05-12
linux下编程出现 对'sem_wait'未定义的引用解决方案 2021-05-12
工具研究:(三)Nginx配置错误的路由时均统一跳转到登录界面 2021-05-12
前端框架(react+umi+dva+ant design pro )攻克: 二、react 父子组件通信(二) 2021-05-12
ant design pro v5去掉右边content区域的水印 2021-05-12
web_求和(练习) 2021-05-12
JavaScript——使用iterator遍历迭代map,set集合元素 2021-05-12
IAR调试卡顿的解决办法 2021-05-13
应用程序无法启动,应用程序的并行配置不正确完美解决方法 2021-05-13
【IntelliJ IDEA 2019.2】idea如何开启自动编译 2021-05-13
强大的文字处理器——Nisus Writer Pro 2021-05-13
如何轻松适应从Windows到MacOS的过渡!Mac新手入门指南 2021-05-13
fcpx插件:25个假日主题专业设计 2021-05-13
fcpx插件:Block Party for Mac(53个视频转场插件) 2021-05-13
Mac使用技巧:快速视频播放错误如何修护 2021-05-13
代码绘制五角形 2021-05-13