java爬虫与python爬虫对比哪个更简单-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

这篇文章将为大家详细讲解有关java爬虫与python爬虫对比哪个更简单,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
java爬虫与python爬虫的对比:
python做爬虫语法更简单,代码更简洁。java的语法比python严格,而且代码也更复杂
示例如下:
url请求:
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 repattern = re.compile(正则) group = pattern.findall(字符串)
关于java爬虫与python爬虫对比哪个更简单就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
名称栏目:java爬虫与python爬虫对比哪个更简单-创新互联
标题来源:http://www.jxjierui.cn/article/jsgee.html


咨询
建站咨询
