日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

行情宝 爬取

發布時間:2024/1/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 行情宝 爬取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

library(rvest)
library(rjson)
library(RMySQL)
##?????
url='http://hqb.nxin.com/hqb/queryPigPrice.shtml'
doc=read_html(url)
href1=doc%>%html_nodes('div[class="pt_city"] a')%>%html_attrs()%>%.[1:31]
sheng_name=doc%>%html_nodes('div[class="pt_city"] a')%>%html_text()%>%.[1:31]
sheng_href=c()
for(i in 1:31) sheng_href=c(sheng_href,href1[[i]])
sheng_area_id=gsub('[^0-9]','',sheng_href);names(sheng_area_id)=c()
sheng=data.frame(area=sheng_name,areaid=sheng_area_id,href=sheng_href,stringsAsFactors = F)


#####??е??
shi=data.frame()
for(jj in 1:31){
URL='http://hqb.nxin.com'
url=paste0(URL,sheng_href[jj])
doc=read_html(url)
href2=doc%>%html_nodes('div[class="pt_city"] a ')%>%html_attrs()
shi_name=doc%>%html_nodes('div[class="pt_city"] a ')%>%html_text()
shi_href=c()
for(i in 1:length(href2)) shi_href=c(shi_href,href2[[i]])
shi_area_id=gsub('[^0-9]','',shi_href);names(shi_area_id)=c()
shi1=data.frame(area=shi_name,areaid=shi_area_id,areapid=rep(sheng_area_id[jj],length(href2)),
areapidname=rep(sheng_name[jj],length(href2)),href=shi_href,stringsAsFactors = FALSE)
shi=rbind(shi,shi1)
}
rm(shi1)
#############################################
shi_todaydata=data.frame()
for(i in 1:363){
URL='http://hqb.nxin.com'
url=paste0(URL,shi$href[i])
doc=read_html(url)
data1=doc%>%html_nodes('table[class="notable t_table"]')%>%html_table() ##
data2=data.frame(data1)
data2$X1=substring(data2$X1,1,23);data2$X2=substring(data2$X2,1,24);data2$X3=substring(data2$X3,1,24);data2$X4=substring(data2$X4,1,17)
data2=gsub('[\u4e00-\u9fa5]','',data2);data2=gsub('\t','',data2);data2=gsub('\n','',data2)
data2=gsub('\\(14%\\)','',data2);data2=gsub('\\(43%\\)','',data2);data2=gsub('\\(\\)','',data2);data2=gsub('\\/','',data2);
todaydate=doc%>%html_nodes('div[class="pt_tit"] ')%>%html_text() ##
todaydate=gsub('\t','',todaydate);todaydate=gsub('\n','',todaydate) ##
todaydate=substring(todaydate,nchar(todaydate)-7,nchar(todaydate)) ###
todaydate=gsub('\\(','',todaydate);todaydate=gsub('\\)','',todaydate)
todaydate=gsub('月','-',todaydate);todaydate=gsub('日','',todaydate);todaydate=paste0('2017-',todaydate)
data3=data.frame(area=shi$area[i],areaid=shi$areaid[i],areapid=shi$areapid[i],pigprice=data2[1],cornprice=data2[2],
beanprice=data2[3],date=todaydate)
shi_todaydata=rbind(shi_todaydata,data3)
}
#########
#library(RMySQL)
con=dbConnect(MySQL(),dbname='henan_xumuwang',host='localhost',port=3306,username='root',password='')
dbSendQuery(con,'set names gbk')
dbWriteTable(con,'pigdata_shi',shi_todaydata,append=TRUE,row.names=FALSE)
dbDisconnect(con)

轉載于:https://www.cnblogs.com/yijiaming/p/10039235.html

總結

以上是生活随笔為你收集整理的行情宝 爬取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。