2022年 11月 4日

java爬虫与python爬虫谁更强?

java爬虫与python爬虫的对比:

python做爬虫语法更简单,代码更简洁。java的语法比python严格,而且代码也更复杂

示例如下:

url请求:

java版的代码如下:

public String call (String url){

  1. String content = "";
  2. BufferedReader in = null;
  3. try{
  4. URL realUrl = new URL(url);
  5. URLConnection connection = realUrl.openConnection();
  6. connection.connect();
  7. in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"gbk"));
  8. String line ;
  9. while ((line = in.readLine()) != null){
  10. content += line + "\n";
  11. }
  12. }catch (Exception e){
  13. e.printStackTrace();
  14. }
  15. finally{
  16. try{
  17. if (in != null){
  18. in.close();
  19. }
  20. }catch(Exception e2){
  21. e2.printStackTrace();
  22. }
  23. }
  24. return content;
  25. }

python版的代码如下:

正则表达式

java版的代码如下:

python的代码如下: