• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

数据挖掘---R语言爬虫(基于hardly的rvest包)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

library(rvest)
library(stringr)

getdata<-function(page,urlwithoutpage){
#读取数据,规定编码
web<-read_html(paste0(urlwithoutpage,page),encoding="GBK")
#获取书名 这些符号(:((—)后面的统统丢掉
titie_all <- web %>% html_nodes("div ul p.name") %>% html_text()
title <- sapply(strsplit(titie_all,split = "[ :((—―]"),"[",2)
#获取价格
price <-web %>% html_nodes("div ul span.search_now_price") %>% html_text()
#获取出版信息
search_book_author <- web %>% html_nodes("p.search_book_author") %>% html_text()
#为避免与CSV的逗号混淆,字段里的逗号统一替换成中文逗号
search_book_author <-gsub(pattern = ",", replacement = ",", search_book_author)
author <- sapply(strsplit(search_book_author,"/"),"[",1)
#利用正则表达式提取字符串
publication_date = str_extract(search_book_author,"\\d{4}-\\d{2}-\\d{2}")
publishing_house = str_extract(search_book_author,"\\w*出版社\\w*")
#整合成数据框返回
data.frame(title,price,author,publication_date,publishing_house)
}

#当当网上检索R语言得到的URL
dangdang <- "http://search.dangdang.com/?key=r%D3%EF%D1%D4&act=input&ddt-rpm=undefined&page_index="
#爬取其中的三页信息
final<-data.frame()
for (i in 1:2){
final<-rbind(final,getdata(i,dangdang))
}
write.table(final, \'dangdang.csv\', sep = ",",row.names = FALSE)

 

 

 

 

python

 #一个简单例子---中国图书网
# 爬虫
# 正则表达式爬虫
import requests
import re

url = \'http://www.bookschina.com/book_find2/?stp=python\'
content = requests.get(url).text
title = re.findall(\'jpg" title="(.*?)"\',content)
author = re.findall(\'作者:.*?;sbook=(.*?)">.*?出版社:\',content)
pubhouses = re.findall(\'出版社:.*?">(.*?)</a><br>出版时间:\',content)
pubtime = re.findall(\'</a><br>出版时间:(.*?) <br>ISBN:\',content)
ISBN = re.findall(\'<br>ISBN:(.*?)<br>原价:\',content)
raw_price = re.findall(\'ISBN:.*?<br>原价:¥(.*?)<br>现价:\',content)
now_price = re.findall(\'现价:¥<span class="red12">(.*?)</span>&nbsp;&nbsp;&nbsp;&nbsp;您节省:\',content)
jiesheng = re.findall(\'您节省:<span class="red12">¥(.*?)</span>\',content)
discount = re.findall(\'您节省:<span class="red12">.*?</span>&nbsp;&nbsp;&nbsp;&nbsp;((.*?)折)<br>\',content)


file = open(\'books.csv\',\'w\')
for i in list(range(0,len(title))):
    file.write(\',\'.join((title[i],author[i],pubhouses[i],pubtime[i],ISBN[i],raw_price[i],now_price[i],jiesheng[i],discount[i]))+\'\n\')
    #print(title[i],author[i],pubhouses[i],pubtime[i],ISBN[i],raw_price[i],now_price[i],jiesheng[i],discount[i])
file.close() 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言爬虫初尝试-基于RVEST包学习发布时间:2022-07-18
下一篇:
R语言爬虫:CSS方法与XPath方法对比(表格介绍)发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap