如何用R语言爬取网页表格数据节省一天工作时间

Python021

如何用R语言爬取网页表格数据节省一天工作时间,第1张

方法/步骤fromurllib.requestimporturlopen用于打开网页fromurllib.errorimportHTTPError用于处理链接异常frombs4importBeautifulSoup用于处理html文档importre用正则表达式匹配目标字符串例子用关于抓取百度新闻网页的某些图片链接fromurllib.requestimporturlopenfromurllib.errorimportHTTPErrorfrombs4importBeautifulSoupimportreurl="/"try:html=urlopen(url)exceptHTTPErrorase:print(e)try:bsObj=BeautifulSoup(html.read())images=bsObj.findAll("img",{"src":re.compile(".*")})forimageinimages:print(image["src"])exceptAttributeErrorase:print(e)importjava.io.BufferedReaderimportjava.io.IOExceptionimportjava.io.InputStreamReaderimportjava.net.HttpURLConnectionimportjava.net.MalformedURLExceptionimportjava.net.URLpublicclassCapture{publicstaticvoidmain(String[]args)throwsMalformedURLException,IOException{StringstrUrl="/"URLurl=newURL(strUrl)HttpURLConnectionhttpConnection=(HttpURLConnection)url.openConnection()InputStreamReaderinput=newInputStreamReader(httpConnection.getInputStream(),"utf-8")BufferedReaderbufferedReader=newBufferedReader(input)Stringline=""StringBuilderstringBuilder=newStringBuilder()while((line=bufferedReader.readLine())!=null){stringBuilder.append(line)}Stringstring=stringBuilder.toString()intbegin=string.indexOf("")intend=string.indexOf("")System.out.println("IPaddress:"+string.substring(begin,end))}

第一部分:数据信息

生活中,我们面临着各种各样的数据:比如你的成绩单,比如公司的财务报表,比如朋友圈的一些状态,比如微信里的一段语音……我们生活的大数据时代的一个重要特征便是数据的多样化(variety)。

也许你期待的数据是这样的:

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species

## 1 5.1 3.5 1.4 0.2 setosa

## 2 4.9 3.0 1.4 0.2 setosa

## 3 4.7 3.2 1.3 0.2 setosa

## 4 4.6 3.1 1.5 0.2 setosa