爬蟲基礎篇
常識介紹
- 目前爬蟲這項技術比如java C++ python PHP都可以支持爬蟲,目前支持最好的是python,用到的框架就是scrapy對於靜態頁面,動態頁面的話就可以用selenium。中間複雜一些就會涉及到登陸 驗證碼,動態渲染,IP問題,加密,app內部抓取等,獲取到的數據主要用於大數據的分析,搜索技術上, 目前我也只是瞭解初級 簡單一些。
爬蟲原理
簡單原理
- 指定一個URL
- 發送Http請求
- 接受響應
- 解析標籤提取數據
技術點主要是3個
- 第一個 : 網絡請求框架 Socket—> HttpURLConnection->HttpClient–>hc flunt 依次進行封裝
- 第二個 : 將HTML二進制文件 轉化爲文本 –JSOUP
- 第三個 : 線程池 隊列 優化技術
模擬瀏覽器網絡請求
*
InputStream inputStream = c*1. 請求百度HttpURLConnection 來源於jdk自帶**`
public static void main(String[] args) throws Exception { String urlstr = "http://www.baidu.com"; URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); //請求方式要大寫 connection.setDoOutput(true); //發送請求 OutputStream outputStream = connection.getOutputStream(); outputStream.write("username=zhangsan&password=123".getBytes("UTF-8")); outputStream.flush(); outputStream.close(); //接受請求onnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while((line=bufferedReader.readLine())!=null) { System.out.println(line); } inputStream.close(); }`
2. 鏈式編程 flunt-HC
` String html = Request.Get(“http://www.baidu.com“).execute().returnContent().asString(Charset.forName(“utf-8”));
System.out.println(html);Request.Post("http://targethost/login") .bodyForm(Form.form().add("username", "vip").add("password", "secret").build()).execute() .returnContent();`
3. HttpClient
CloseableHttpClient createDefault = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.baidu.com"); //參數拼接 ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>(); arrayList.add(new BasicNameValuePair("admin", "admin")); arrayList.add(new BasicNameValuePair("admin", "admin")); httpPost.setEntity(new UrlEncodedFormEntity(arrayList)); CloseableHttpResponse execute = createDefault.execute(httpPost); String code = EntityUtils.toString(execute.getEntity(), Charset.forName("utf-8"));
Http狀態碼
http協議狀態碼
200 302 304 404 500- 1xx 創建
- 2xx 成功
- 3xx 重定向
- 4xx 客戶端錯誤
- 5xx 服務器錯誤
解析爬蟲爬取回來的數據
- 解析HTML主要是用Jsoup,它一款專門用來解析HTML標籤的解析器,內部封裝了API
Select方法將返回一個Elements集合,並提供一組方法來抽取和處理結果。
- Selector選擇器概述
- tagname: 通過標籤查找元素,比如:a
- ns|tag: 通過標籤在命名空間查找元素,比如:可以用 fb|name 語法來查找 元素
- id: 通過ID查找元素,比如:#logo
- class: 通過class名稱查找元素,比如:.masthead
- [attribute]: 利用屬性查找元素,比如:[href]
- [^attr]: 利用屬性名前綴來查找元素,比如:可以用[^data-] 來查找帶有HTML5 Dataset屬性的元素
- [attr=value]: 利用屬性值來查找元素,比如:[width=500]
- [attr^=value], [attr$=value], [attr*=value]: 利用匹配屬性值開頭、結尾或包含屬性值來查找元素,比如:[href*=/path/]
- [attr~=regex]: 利用屬性值匹配正則表達式來查找元素,比如: img[src~=(?i).(png|jpe?g)]
- *: 這個符號將匹配所有元素
- Seector選擇器組合使用
- el#id: 元素+ID,比如: div#logo
- el.class: 元素+class,比如: div.masthead
- el[attr]: 元素+class,比如: a[href]
- 任意組合,比如:a[href].highlight
- ancestor child: 查找某個元素下子元素,比如:可以用.body p 查找在”body”元素下的所有 p元素
- parent > child: 查找某個父元素下的直接子元素,比如:可以用div.content > p 查找 p 元素,也可以用body > * 查找body標籤下所有直接子元素
- siblingA + siblingB: 查找在A元素之前第一個同級元素B,比如:div.head + div
- siblingA ~ siblingX: 查找A元素之前的同級X元素,比如:h1 ~ p
- el, el, el:多個選擇器組合,查找匹配任一選擇器的唯一元素,例如:div.masthead, div.logo
- 僞選器selectors
- :lt(n): 查找哪些元素的同級索引值(它的位置在DOM樹中是相對於它的父節點)小於n,比如:td:lt(3) 表示小於三列的元素
- :gt(n):查找哪些元素的同級索引值大於n,比如: div p:gt(2)表示哪些div中有包含2個以上的p元素
- :eq(n): 查找哪些元素的同級索引值與n相等,比如:form input:eq(1)表示包含一個input標籤的Form元素
- :has(seletor): 查找匹配選擇器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素
- :not(selector): 查找與選擇器不匹配的元素,比如: div:not(.logo) 表示不包含 class=logo 元素的所有 div 列表
- :contains(text): 查找包含給定文本的元素,搜索不區分大不寫,比如: p:contains(jsoup)
- :containsOwn(text): 查找直接包含給定文本的元素
- :matches(regex): 查找哪些元素的文本匹配指定的正則表達式,比如:div:matches((?i)login)
- :matchesOwn(regex): 查找自身包含文本匹配指定正則表達式的元素
- 注意:上述僞選擇器索引是從0開始的,也就是說第一個元素索引值爲0,第二個元素index爲1等
Jsoup的maven依賴
```java
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
```