
本文共 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
���������������������������������
- Rank: ������
- Title���������������
- Runtime���������������
- Genre���������������
- Rating���������������
- Metascore���������������
- Description���������������
- Votes������������������������������
- 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���������������������������
- ������������������ Chrome ��������������������������������������� F12 ���������������������
- ���������������������������������������������������������������������������
- ���������������������������������������������������������������
- ������ 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)
������������������������������������������������������������������������������
##### >> Notesrvest
������������������������������������������������������
html_tag()
: ������DOM ��� tag namehtml_attr()
: ������DOM ��� ������������html_attrs()
: ������DOM ��� ������������guess_encoding()
andrepair_encoding()
��� ��������������������������� ���������������������������������������������~���jump_to()
,follow_link()
,back()
,forward()
: ���������������������������������������������
发表评论
最新留言
关于作者
