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

news/2024/7/19 9:50:14 标签: 爬虫, r语言, python

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() 

转载于:https://www.cnblogs.com/heisaijuzhen/p/6656439.html


http://www.niftyadmin.cn/n/820519.html

相关文章

c语言指针指针变量scanf,C语言指针练习(包括答案和详解)

《C语言指针练习(包括答案和详解)》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《C语言指针练习(包括答案和详解)(21页珍藏版)》请在人人文库网上搜索。1、第7章第7章指针71选择题1说明&#xff1a;int a2&#xff0c;*p&#xff0c;以下错误的赋值语句为(D):a pq&a…

又一个程序员,被抓捕!(真实事件)

这篇文章是公号一位程序员读者的投稿&#xff0c;整个过程就是他自身的经历&#xff0c;文中涉及到的一些敏感点&#xff0c;进行了模糊处理。文章中的“我”为作者本人。1你们张总在吗&#xff1f;今年的冬天&#xff0c;小明日常到朋友公司蹭个位置坐点自己的事情&#xff0c…

Hibernate的配置与简单使用

一、概念 Hibernate是一个开源的对象关系映射&#xff08;ORM&#xff09;框架&#xff0c;它对JDBC进行了非常轻量级的对象封装&#xff0c;使得Java程序员可以抛弃在程序中编写SQL语句&#xff0c;随心所欲的使用对象编程思维来操纵数据库。 二、准备工作 在Eclipse中安装Hib…

枪林弹雨,年薪百万,我在非洲当程序员,好刺激...

本文原创公众号&#xff1a;不会笑青年&#xff0c;授权转载请联系微信(laughyouth369)&#xff0c;授权后&#xff0c;请在原创发表48小时后再转载。< END >作者&#xff1a;不会笑青年漫画师&#xff1a;Summer点击右上角 "..." 设为星标&#xff0c;可第一时…

C语言编程图书销售系统,简单的书店图书销售系统求指点

已结贴√问题点数&#xff1a;18 回复次数&#xff1a;1简单的书店图书销售系统求指点1. 功能(需求)分析书店为了能实现日常的经营&#xff0c;经常涉及到到事物及应该具有的功能是&#xff1a;图书入库(进书采购时)、图书销售、查看图书的库存情况(清单)、图书删除、数据保存、…

我28岁,人生第一封遗书...

我们常常高呼“自由万岁”&#xff0c;但其实&#xff0c;绝大多数人本质上是不想要自由的&#xff0c;猛一听是不是非常不赞同呢&#xff1f;鉴于漫画原创的300字数限制&#xff0c;请在不会笑青年公号内回复“自由”&#xff0c;查看具体内容。本篇漫画改编于一个真实的故事&…

select选择不同的option div发生变化_elementui中select实现下拉分页和关键字搜索

当select的数据太多时&#xff0c;查询很慢&#xff0c;并且样式不美观&#xff0c;需要实现分页功能。如果很多页面都需要分页下拉&#xff0c;新建一个组件。代码如下&#xff1a;<template><el-select v-model"copyValue":disabled"disabled"f…

MongoDB(二)-- Java API 实现增删改查

一、下载jar包 http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ 二、代码实现 1 package com.xbq.mongodb;2 import java.util.ArrayList;3 import java.util.HashMap;4 import java.util.List;5 import java.util.Map;6 import org.bson.types.ObjectId;7 im…