暂无分类
暂无标签
发布于2020-11-09 18:19 阅读(337) 评论(0) 点赞(18) 收藏(1)
0
1
2
3
4
5
很多人说学习数据挖掘,先从爬虫入手。接触了大大小小的项目后,发现数据的获取是数据建模前的一项非常重要的活儿。在此,我需要先总结一些爬虫的流程,分别有python版的以及java版的。
java版的代码如下:
- public String call (String url){
- String content = "";
- BufferedReader in = null;
- try{
- URL realUrl = new URL(url);
- URLConnection connection = realUrl.openConnection();
- connection.connect();
- in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"gbk"));
- String line ;
- while ((line = in.readLine()) != null){
- content += line + "\n";
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- finally{
- try{
- if (in != null){
- in.close();
- }
- }catch(Exception e2){
- e2.printStackTrace();
- }
- }
- return content;
- }
python版的代码如下:
- # coding=utf-8
- import chardet
- import urllib2
-
- url = "http://www.baidu.com"
- data = (urllib2.urlopen(url)).read()
- charset = chardet.detect(data)
- code = charset['encoding']
- content = str(data).decode(code, 'ignore').encode('utf8')
- print content
java版的代码如下:
- public String call(String content) throws Exception {
- Pattern p = Pattern.compile("content\":\".*?\"");
- Matcher match = p.matcher(content);
- StringBuilder sb = new StringBuilder();
- String tmp;
- while (match.find()){
- tmp = match.group();
- tmp = tmp.replaceAll("\"", "");
- tmp = tmp.replace("content:", "");
- tmp = tmp.replaceAll("<.*>", "");
- sb.append(tmp + "\n");
- }
- String comment = sb.toString();
- return comment;
- }
- }
python的代码如下:
- import re
- pattern = re.compile(正则)
- group = pattern.findall(字符串)
python学习资料领取看主页简介或添加q裙467604262一起交流探讨
原文链接:https://blog.csdn.net/xixi20200/article/details/109556292
0
1
2
3
4
5
6
7
8
9
作者:9384vfnv
链接: https://www.pythonheidong.com/blog/article/611712/760b9108d9de8a717f4d/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2019 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系z452as@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!