爬虫基础篇
常识介绍
- 目前爬虫这项技术比如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>
```