創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!
這篇文章將為大家詳細講解有關(guān)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(字符串)
關(guān)于java爬蟲與python爬蟲對比哪個更簡單就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁題目:java爬蟲與python爬蟲對比哪個更簡單-創(chuàng)新互聯(lián)
URL標題:http://chinadenli.net/article44/jsgee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、Google、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容