我想在R中自动执行以下任务:
使用历史数据转到以下页面: http://www.ariva.de/XXX/historische_kurse, XXX代表一些自动收报机,例如DBX0BT
所以这种情况下的URL是: http://www.ariva.de/DBX0BT/historische_kurse
右下角有一个按钮<div id='cssmenu'> <!-- Containter For Nav Menu -->
<ul>
<li><strong>Browse Our Products</strong></li> <!--List Title-->
<li><a href='http://www.offsetprinting.com/products/businesscards'>Business Cards</a></li>
<li class='active has-sub'><a href='#'>Stationary</a>
<ul>
<li class=''><a href='http://www.offsetprinting.com/products/letterhead'>Letter Head</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/envelopes4color'>Envelopes</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/labels'>Labels</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/notepads'>Note Pads</a></li>
</ul>
</li>
<li><a href='http://www.offsetprinting.com/products/brochures'>Brochures</a></li>
<li class='active has-sub'><a href='#'>Signage</a>
<ul>
<li class=''><a href='http://www.offsetprinting.com/products/signage'>Signs</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/yard-signs'>Yard signs</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/trade-show-graphics'>Trade Show Graphics</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/x-frame-with-banner'>X-Frame With Banners</a></li>
</ul>
</li>
<li><a href='http://www.offsetprinting.com/products/postcards'>Post Cards</a></li>
<li><a href='http://www.offsetprinting.com/products/vinylbanners'>Vinyl Banners</a></li>
<li class='active has-sub'><a href='#'>Mailing Services</a>
<ul>
<li class=''><a href='http://www.offsetprinting.com/mailing/postcards-mailing'>Ink Jet</a></li>
<li class=''><a href='http://www.offsetprinting.com/mailing/postcards-mailing'>Direct Mailing</a></li>
<li class=''><a href='http://www.offsetprinting.com/mailing/postcards-mailing'>Sorting & Tabbing</a></li>
<li class=''><a href='http://www.offsetprinting.com/mailing/postcards-mailing'>Mailing List</a></li>
</ul>
</li>
<li class='active has-sub'><a href='#'>Marketing Material</a>
<ul>
<li class=''><a href='http://www.offsetprinting.com/products/greetingcards'>Greeting Cards</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/rackcards'>Rack Cards</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/brochures'>Brochures</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/bookmarks'>Bookmarks</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/folders'>Presentation Folders</a></li>
</ul>
</li>
<li class='active has-sub'><a href='#'>Speciality Products</a>
<ul>
<li class=''><a href='http://www.offsetprinting.com/products/magnets-digital'>Magnets</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/tshirts-digital'>T-Shirts (Heat Transfer Imaging)</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/bumperstickers-digital'>Bumper Stickers</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/plastic-digital'>Plastic</a></li>
<li class=''><a href='http://www.offsetprinting.com/products/staticcling-digital'>Static Cling</a></li>
</ul>
</li>
<li><a href='https://www.apparelcatalog.net/Default.aspx?site=XCHWEUPRWS'>Apparel</a> </li>
</ul>
</div><!-- End of CSS Menu -->
。
我想创建一个输入的函数:
然后通过“按Download
按钮”下载生成的csv文件到路径给出的位置路径。
我的问题
我怎么能在R?中做到这一点?
答案 0 :(得分:3)
只要您有WKN或ISIN的列表,就可以运行此列表并读取/下载文件。你也可以刮掉WKN / ISIN,但这个网页会更复杂,我假设你可以访问这些信息。
library(XML)
wkn<-c("DBX0BT","865985") #some stock IDs (WKN) for your targets
date.F<-"12.5.2014" #from when for historical data
date.T<-"12.5.2015" #to when
for(j in 1:length(wkn)){
tg<-htmlParse(paste0("http://www.ariva.de/",wkn[j],"/historische_kurse")) #parse the historical webpage
atbts<-xpathSApply(tg,'//div[@class="content left abstand"]/input') #extract attributes for that stock so we can download it
secu<-sapply(atbts, function(x) xmlAttrs(x)['value'])[1] #security id (only relevant for downloading)
boerseid<-sapply(atbts, function(x) xmlAttrs(x)['value'])[2] #boerse id (only relevant for downloading)
#download, read and assign the values
assign(wkn[j],read.csv(paste0("http://www.ariva.de/quote/historic/historic.csv?secu=",secu,"&boerse_id=",boerseid,
"&clean_split=1&clean_payout=0&clean_bezug=1&min_time=",date.F,"&max_time=",date.T,"&trenner=%3B&go=Download"),
sep=";"))
write.csv(get(wkn[j]),paste0(wkn[j],".csv"))
}