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版的代码如下:
正则表达式
java版的代码如下:
python的代码如下: