您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java怎么爬取網頁內容并輸出到Excel中的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java怎么爬取網頁內容并輸出到Excel中文章都會有所收獲,下面我們一起來看看吧。
網絡爬蟲(Web Crawler),按照一定的規則,自動抓取萬維網信息的程序或者腳本,如今被廣泛地應用在互聯網搜索引擎或者其他類似網站。
爬蟲在功能上分為采集、處理和儲存三個部分。
爬蟲基本上可以分為三大類:分布式爬蟲、Java爬蟲以及非Java爬蟲。
在Java爬蟲中又可以細分出三種,Crawler4j、WebMagic、WebCollector。
添加依賴
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--excel-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!--爬蟲-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
創建一個Weather
實體類
public class Weather {
/**
* 日期
*/
private String date;
/**
* 最高氣溫
*/
private String maxTemperature;
/**
* 最低氣溫
*/
private String minTemperature;
/**
* 白天天氣
*/
private String dayTimeWeather;
/**
* 夜間天氣
*/
private String nightWeather;
/**
* 風向
*/
private String windDirection;
/**
* 風力
*/
private String windPower;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMaxTemperature() {
return maxTemperature;
}
public void setMaxTemperature(String maxTemperature) {
this.maxTemperature = maxTemperature;
}
public String getMinTemperature() {
return minTemperature;
}
public void setMinTemperature(String minTemperature) {
this.minTemperature = minTemperature;
}
public String getDayTimeWeather() {
return dayTimeWeather;
}
public void setDayTimeWeather(String dayTimeWeather) {
this.dayTimeWeather = dayTimeWeather;
}
public String getNightWeather() {
return nightWeather;
}
public void setNightWeather(String nightWeather) {
this.nightWeather = nightWeather;
}
public String getWindDirection() {
return windDirection;
}
public void setWindDirection(String windDirection) {
this.windDirection = windDirection;
}
public String getWindPower() {
return windPower;
}
public void setWindPower(String windPower) {
this.windPower = windPower;
}
@Override
public String toString() {
return "Weather{" +
"date='" + date + '\'' +
", maxTemperature='" + maxTemperature + '\'' +
", minTemperature='" + minTemperature + '\'' +
", dayTimeWeather='" + dayTimeWeather + '\'' +
", nightWeather='" + nightWeather + '\'' +
", windDirection='" + windDirection + '\'' +
", windPower='" + windPower + '\'' +
'}';
}
}
創建一個WeatherTest
測試類
public class WeatherTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
List<Weather> list = getInfo("http://www.tianqi234.com/2020shanghai/1yue.html", 12);
for (Weather weather : list) {
System.out.println(weather.toString());
}
testHSSFWorkbook(list);
}
//可以指定網址,并且按照需求爬取前多少頁的數據
public static List<Weather> getInfo(String url, int month) {
List<Weather> weatherList = new ArrayList<Weather>();
for (int i = 1; i < month + 1; i++) {
try {
System.out.println("url:" + url);
Document doc = Jsoup.connect(url).get();
Elements table = doc.select(".graybox_cnt");
/* Elements tbody = table.select("tbody");*/
Elements trList = table.select("tr");
//每次移除的時候,你的列表長度就會發生新的變化,所以要結合實際進行移除
trList.remove(0);
if (i > 1) {
trList.remove(0);
trList.remove(10);
trList.remove(10);
trList.remove(20);
trList.remove(20);
trList.remove(20);
} else {
trList.remove(11);
trList.remove(11);
trList.remove(21);
trList.remove(21);
trList.remove(21);
}
for (Element tr : trList) {
Elements tdList = tr.select("td");
Elements aList = tdList.select("a");//查詢a標簽
Weather weather = new Weather();
if (aList != null && aList.size() > 0) {
weather.setDate(aList.get(0).html().toString());
} else {
weather.setDate(tdList.get(0).html().toString());
}
weather.setMaxTemperature(tdList.get(1).html().toString());
weather.setMinTemperature(tdList.get(2).html().toString());
weather.setDayTimeWeather(tdList.get(3).html().toString());
weather.setNightWeather(tdList.get(4).html().toString());
weather.setWindDirection(tdList.get(5).html().toString());
weather.setWindPower(tdList.get(6).html().toString());
weatherList.add(weather);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
url = "http://www.tianqi234.com/2020shanghai/" + (i + 1) + "yue.html";
}
return weatherList;
}
public static void testHSSFWorkbook(List<Weather> list) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();//創建excel文件(workbook)
HSSFSheet sheet = workbook.createSheet("2020年上海天氣統計");
HSSFRow row = sheet.createRow(0);//創建行 從0開始
HSSFCellStyle style = workbook.createCellStyle();//設置單元格樣式
style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sheet.setDefaultColumnWidth(30);
row.setHeightInPoints(25);
Map<String, String> map = (Map<String, String>) getMap(list.get(0));
//設置表頭
int c = 0;
for (String key : map.keySet()) {
HSSFCell cell = row.createCell(c);//創建行的單元格,從0開始
cell.setCellValue(map.get(key));//設置單元格內容
cell.setCellStyle(style);
c++;
}
Map<Integer, Weather> weatherMap = new HashMap<>();
//除去表頭
for (int i = 1; i < list.size(); i++) {
weatherMap.put(i, list.get(i));
}
for (int i = 1; i <= weatherMap.size(); i++) {
HSSFRow rowInfo = sheet.createRow(i);
rowInfo.setHeightInPoints(30);
Map<String, String> map1 = (Map<String, String>) getMap(list.get(i));
int j = 0;
for (String key : map1.keySet()) {
HSSFCell cellInfo = rowInfo.createCell(j);
cellInfo.setCellValue(map1.get(key));
cellInfo.setCellStyle(style);
j++;
}
}
FileOutputStream out = new FileOutputStream("D:\\weather1.xlsx");
workbook.write(out);
out.close();
}
/**
* json轉map
*
* @param object
* @return
*/
public static Map<?, ?> getMap(Object object) {
if (object == null) {
throw new RuntimeException("對象為空,轉json失敗");
}
Map<String, Object> map = new HashMap<>();
try {
map = (Map) JSON.parse(JSON.toJSONString(object));
} catch (Exception e) {
System.out.println("對象轉map轉換失敗");
}
return map;
}
}
關于“Java怎么爬取網頁內容并輸出到Excel中”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java怎么爬取網頁內容并輸出到Excel中”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。