楼主: 韩佳容
1514 0

[R] rvest爬取数据翻页问题 [推广有奖]

  • 0关注
  • 1粉丝

等待验证会员

已卖:174份资源

本科生

34%

还不是VIP/贵宾

-

威望
0
论坛币
525 个
通用积分
1.9518
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
375 点
帖子
13
精华
0
在线时间
148 小时
注册时间
2018-10-3
最后登录
2025-12-22

楼主
韩佳容 发表于 2019-3-16 22:37:52 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
各位老师,我在用rvest爬取网页数据时,遇到翻页问题,不知道该如何设置循环来一次性抓取,希望各位老师指点:
以下是我做的几次尝试,但是页面仍然只是显示一页。
(1)
n=seq(0,860,length=44)
page=function(n)
{
  url=str_c('https://yz.chsi.com.cn/sch/?start=',n)
  web=read_html(url)
  university=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(1) > a')%>%html_text()
  location=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(2)')%>%html_text()
  subo=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(3)')%>%html_text()
  link=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(7) > a')%>%html_attrs()
  site=rep('https://yz.chsi.com.cn/sch/?start=0',20*43)
  link1=paste(site,link,sep = '')
  messages=data.frame(university,location,subo,link1)
  return(messages)
}

(2)
page=function(n)
{
  x=seq(0,860,length=44)
  s=c(x)
  url=str_c('https://yz.chsi.com.cn/sch/?start=',s[[n]])
  web=read_html(url)
  university=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(1) > a')%>%html_text()
  location=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(2)')%>%html_text()
  subo=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(3)')%>%html_text()
  link=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(7) > a')%>%html_attrs()
  site=rep('https://yz.chsi.com.cn/sch/?start=',20*43)
  link1=paste(site,link,sep = '')
  messages=data.frame(university,location,subo,link1)
  return(messages)
  for (n in 1:length(s)) {
    messages=rbind2(messages,page(n))
  }
}

(3)
x=seq(0,860,length=44)
s=c(x)
i=1
for (n in 1:length(s)) {
  page=function(i){
    url=str_c('https://yz.chsi.com.cn/sch/?start=',s[i])
    web=read_html(url)
    university=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(1) > a')%>%html_text()
    location=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(2)')%>%html_text()
    link=html_nodes(web,'body > div.main-wrapper > div.container > div.yxk-table > table > tbody > tr > td:nth-child(7) > a')%>%html_attrs()
    site=rep('https://yz.chsi.com.cn/sch/',20)
    link1=paste(site,link,sep = '')
    messages=data.frame(university,location,link1)
    messagess=rbind2(messages,page(i))
  }
}


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-2-16 02:12