Java爬蟲 頁面跳轉爬資料

2021-08-07 13:14:13 字數 1305 閱讀 3908

webdriver driver = new htmlunitdriver(); 

string url = ""

; driver.get(url);

driver.findelement(by.id("kw")).sendkeys(keyword);

driver.findelement(by.id("su")).click();

這裡的keyword是自己輸入的引數,**中沒寫全,搜尋後的頁面應該是這樣的:

partiallinktext()函式是匹配部分鏈結文字,這裡driver到達了中關村的頁面了,如下:

繼續跳轉到找到的這個鏈結頁面,**如下:

string href_can =driver.findelement(by.partiallinktext("引數")).getattribute("href");

driver.get(href_can);

webelement tb = driver.findelement(by

.id("newtb"));

list

list

= tb.findelements(by

.tagname("li"));

for(webelement e : list) else

}if(tag)

}

}

上面的**中用到了簡單的正規表示式,不懂得童鞋可以自己去學習一下,以後都很有用的。別忘了最後還需要停止driver。**是:

driver.quit();
到這裡爬引數的資料基本就做完了,**核心部分都寫全了,還有些沒有寫,比如輸入什麼的,還有異常處理也沒有寫,這個學習者自己去學習,然後可以看到我的**基本都沒有注釋,其實是有的,我在這裡面都刪掉了,就是希望大家自己多查資料學習,我自己也只是個學習者,希望本篇文章能對各位有幫助。

requests爬蟲爬取頁面資料

新建檔案test.py,寫入一下 import requests 通過pip install requests安裝 from bs4 import beautifulsoup 通過pip install bs4安裝 import re 安裝了python就有了re模組 import json 安裝了...

爬爬爬,爬蟲之獲取資料 requests

推薦使用requests庫,相比urllib使用要簡介的多 requests向目標站點傳送請求,獲得乙個httpresponse響應 import requests requests.get requests.post requests.put requests.delete requests.he...

python爬蟲之JS鏈結跳轉內容爬取

目標 民政資料 行政區劃 即 http article sj xzqh 2019 2 目標 抓取最新中華人民共和國縣以上行政區劃 實現步驟 特點 1 最新的在上面 2 命名格式 2019年x月中華人民共和國縣以上行政區劃 實現 0 正則匹配title中包含這個字串的鏈結 if re.findall ...