java多个页面爬取_java爬取html页面(简易通用版)
項目環境
ide:intellij IDEA 2017.2.5
編程語言:java
數據庫:mysql
管理工具:maven
需要導包:mysql-connector-java,jsoup(解析html)等,如果要解析json可能還要導入gson的包。
我把重要的依賴貼出來(默認大家都是使用過maven的,如果不使用maven,你可以到網上下載jar包添加到項目里):
com.google.code.gson
gson
2.8.0
mysql
mysql-connector-java
5.1.38
org.jsoup
jsoup
1.9.2
代碼解析html頁面
我是以boss直聘網搜索職業為html爬取的
如:https://www.zhipin.com/job_detail/?query=java&city=101020100&industry=&position=
public static void main(String[] args) throws IOException {
Boss boss = new Boss();
//輸入要爬取的頁面
String url = "https://www.zhipin.com/job_detail/?query=java&city=101020100&industry=&position=";
System.out.println(url);
try {
// 添加時間間隔 5s 解決 418問題。
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//解析html
Document doc = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36").get();
System.out.println(doc);
Elements listDiv = doc.getElementsByAttributeValue("class", "company-text");
for (Element text : listDiv) {
Elements a = text.getElementsByTag("a");
String href = a.get(0).attr("href");
String ka= a.get(0).attr("ka");
String span = a.text();
System.out.println(href);
System.out.println(ka);
System.out.println(span);
String url2 = "https://www.zhipin.com"+href+"?ka="+ka;
Document document = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").get();
Elements listdiv = document.getElementsByAttributeValue("class", "business-detail");
for (Element text1 : listdiv) {
Elements li = text1.getElementsByTag("li");
String legalperson = li.get(0).text();
String capital = li.get(1).text();
String province = li.get(5).text();
System.out.println(legalperson);
System.out.println(capital);
System.out.println(province);
}
}
}
爬取boss直聘網需謹慎,隨時封ip
總結
以上是生活随笔為你收集整理的java多个页面爬取_java爬取html页面(简易通用版)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: java ecdh秘钥交换_DH密钥交换
- 下一篇: 成都大熊猫基地下午去能看到熊猫吗
