rm(list = ls())
# 加载包
library('xml2')
library('rvest')
#广东省爬取
web<-read_html("http://www.agri.com.cn/company/township_city_index.asp?province_id=440000",
encoding="gb2312")
#广东省城市链接获取
position<-web %>% html_nodes("a") %>% html_attr('href')
test1 <- data.frame(position)
test2 <- data.frame(test1[19:39,])
colnames(test2) <- c('urls')
test2$urls <- as.character(test2$urls)
n1 <- nrow(test2)
binddata <- data.frame(1)
colnames(binddata) <- c('phone1')
for(i in 1:n1){
#获取第一个城市链接
url1 <- test2$urls[i]
web1 <- read_html(url1,encoding = 'gb2312')
position1 <-web1 %>% html_nodes("a") %>% html_attr('href')
test11 <- data.frame(position1)
colnames(test11) <- c('urls')
#获取第一个城市下,所有城镇乡村电话
test11$urlsbz <- grepl(pattern = '/town/',test11$urls)
test22 <- test11$urls[which(test11$urlsbz == TRUE)]
test22 <- data.frame(test22)
colnames(test22) <- c('urls')
test22$urls <- as.character(test22$urls)
n2 <- nrow(test22)
for(j in 1:n2){
#第一个乡村界面
url1_1 <- test22$urls[j]
web1_1 <- read_html(url1_1,encoding = 'gbk')
position2 <- web1_1 %>% html_nodes("a") %>% html_attr('href')
test33 <- data.frame(position2)
colnames(test33) <- c('urls')
test33$urls <- as.character(test33$urls)
#获取出电话url
num1 <- grep(pattern = '/tel/',test33$urls)
url1_2 <- test33$urls[num1]
#电话url进行调转
web1_2 <- read_html(url1_2,encoding = 'gbk')
phone1 <- web1_2 %>%
html_nodes(css = 'tr:nth-child(2)') %>% html_text()
phone1 <- data.frame(phone1)
phone1$phone1 <- as.character(phone1$phone1)
binddata <- rbind(binddata,phone1$phone1[6])
# 加载包
library('xml2')
library('rvest')
#广东省爬取
web<-read_html("http://www.agri.com.cn/company/township_city_index.asp?province_id=440000",
encoding="gb2312")
#广东省城市链接获取
position<-web %>% html_nodes("a") %>% html_attr('href')
test1 <- data.frame(position)
test2 <- data.frame(test1[19:39,])
colnames(test2) <- c('urls')
test2$urls <- as.character(test2$urls)
n1 <- nrow(test2)
binddata <- data.frame(1)
colnames(binddata) <- c('phone1')
for(i in 1:n1){
#获取第一个城市链接
url1 <- test2$urls[i]
web1 <- read_html(url1,encoding = 'gb2312')
position1 <-web1 %>% html_nodes("a") %>% html_attr('href')
test11 <- data.frame(position1)
colnames(test11) <- c('urls')
#获取第一个城市下,所有城镇乡村电话
test11$urlsbz <- grepl(pattern = '/town/',test11$urls)
test22 <- test11$urls[which(test11$urlsbz == TRUE)]
test22 <- data.frame(test22)
colnames(test22) <- c('urls')
test22$urls <- as.character(test22$urls)
n2 <- nrow(test22)
for(j in 1:n2){
#第一个乡村界面
url1_1 <- test22$urls[j]
web1_1 <- read_html(url1_1,encoding = 'gbk')
position2 <- web1_1 %>% html_nodes("a") %>% html_attr('href')
test33 <- data.frame(position2)
colnames(test33) <- c('urls')
test33$urls <- as.character(test33$urls)
#获取出电话url
num1 <- grep(pattern = '/tel/',test33$urls)
url1_2 <- test33$urls[num1]
#电话url进行调转
web1_2 <- read_html(url1_2,encoding = 'gbk')
phone1 <- web1_2 %>%
html_nodes(css = 'tr:nth-child(2)') %>% html_text()
phone1 <- data.frame(phone1)
phone1$phone1 <- as.character(phone1$phone1)
binddata <- rbind(binddata,phone1$phone1[6])