第一个入手的爬虫小任务:

maven工程

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhaowu</groupId>
<artifactId>pachong01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> </dependencies>
</project>

代码实现:

package com.zhaowu.renwu1;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; public class News {
public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建HttpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpget实例
HttpGet httpGet = new HttpGet("https://news.baidu.com/"); RequestConfig config = RequestConfig.custom()
.setConnectTimeout(10000)//设置连接超时时间10秒钟,单位毫秒
.setSocketTimeout(10000) //设置读取超时时间10秒钟
.build();
httpGet.setConfig(config);
// 设置请求头消息User-Agent模拟浏览器
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/59.0");
// 执行get请求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 获取返回实体
HttpEntity entity = response.getEntity();
// 实体的内容(编码格式为utf-8)
String content = EntityUtils.toString(entity, "utf-8");
// System.out.println("网页内容为: " + content); // 解析网页 得到文档对象
Document doc = Jsoup.parse(content); Elements hrefElements = doc.select("a[href]");// 选择所有的a元素
for (Element e : hrefElements) {
System.out.println("新闻标题:" + e.text());
System.out.println("新闻地址:" + e.attr("href"));
System.out.println("------------------------");
} }
}

爬虫任务一:使用httpclient去爬取百度新闻首页的新闻标题和url,编码是utf-8的更多相关文章

  1. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  2. Python 爬虫实例(1)—— 爬取百度图片

    爬取百度图片  在Python 2.7上运行 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: loveNight import jso ...

  3. Python爬虫实例(一)爬取百度贴吧帖子中的图片

    程序功能说明:爬取百度贴吧帖子中的图片,用户输入贴吧名称和要爬取的起始和终止页数即可进行爬取. 思路分析: 一.指定贴吧url的获取 例如我们进入秦时明月吧,提取并分析其有效url如下 http:// ...

  4. Python 爬虫实例(15) 爬取 百度百聘(微信公众号)

    今天闲的无聊,爬取了一个网站,百度百聘,仅供学习参考 直接上代码: #-*-coding:utf-8-*- from common.contest import * def spider(): hea ...

  5. 使用scrapy爬虫,爬取今日头条首页推荐新闻(scrapy+selenium+PhantomJS)

    爬取今日头条https://www.toutiao.com/首页推荐的新闻,打开网址得到如下界面 查看源代码你会发现 全是js代码,说明今日头条的内容是通过js动态生成的. 用火狐浏览器F12查看得知 ...

  6. python爬虫实战(2)--爬取百度贴吧

    本篇目标 1.对百度贴吧的任意帖子进行抓取 2.指定是否只抓取楼主发帖内容 3.将抓取到的内容分析并保存到文件 1.URL格式的确定 先观察百度贴吧url格式,以中南财经政法大学迎新帖为例,URL我们 ...

  7. Python 爬虫实例(14) 爬取 百度音乐

    #-*-coding:utf-8-*- from common.contest import * import urllib def spider(): song_types = ['新歌','热歌' ...

  8. 百度图片爬虫-python版-如何爬取百度图片?

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  9. 利用python的爬虫技术爬取百度贴吧的帖子

    在爬取糗事百科的段子后,我又在知乎上找了一个爬取百度贴吧帖子的实例,为了巩固提升已掌握的爬虫知识,于是我打算自己也做一个. 实现目标:1,爬取楼主所发的帖子 2,显示所爬去的楼层以及帖子题目 3,将爬 ...

随机推荐

  1. 跟着百度学PHP[15]-session回收机制

    gc(Garbage Collection 垃圾回收) 在用户访问的时候会生成许多的临时session文件,顾名思义session回收机制就是用来删除这些临时文件的. session.gc_maxli ...

  2. lua工具库penlight--06数据(一)

    这篇太长了,分了两部分.(这个是机器翻译之后我又校对了一下,以后的都这样,人工翻译太累了.) 读数据文件 首先考虑清楚,你的确需要一个自定义的文件读入器吗?如果是,你能确定有能力写好吗? 正确,稳健, ...

  3. VC编译的时候因为加载符号导致编译过程异常缓慢的处理

    之前调试console程序用了一下Ctrl+C,弹出的异常处理选择框自己点了什么忘记了,结果之后再调试,发现特别慢,最下面一直在走着一系列dll的加载符号.让我特别二呼的是断网的话就没事了,联网就一直 ...

  4. IPMI特点和功能

    IPMI独立于操作系统外自行运作,并容许管理者即使在缺少操作系统或系统管理软件.或受监控的系统关机但有接电源的情况下仍能远程管理系统. ipmi可以实现对机器的操作举例如下: 开机,关机,重启,查看机 ...

  5. vi 的使用,很详细

    来源:鸟哥的Linux私房菜 网址:http://vbird.dic.ksu.edu.tw/linux_basic/0310vi_2.php

  6. hdu 1164:Eddy's research I(水题,数学题,筛法)

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. Ubuntu安装qBittorrent

    qBitTorrent是Ubuntu Linux中最受欢迎的P2P软件之中的一个. 出自一名法国大学生之手的qBitTorrent功能强大.界面精美.操作直观. qBitTorrent是Linux中最 ...

  8. OpenSSL Heart Bleed 如何修复

     一 . 前言  这两天这个事件沸沸扬扬啊,有了这个bug黑客在电脑前动动手指就能获取各大电商网站.各大银行用户的用户名和密码了,屌爆了 BUG具体内容 : http://heartbleed.com ...

  9. Date类、DateFormat类和Calendar类

    1.Date类 常用方法:long getTime():返回1970年1月1日00:00:00以来的毫秒值,把日期对象转换成毫秒值 2.DateFormat类 DateFormat类是日期/时间格式化 ...

  10. 调结者(Dispatcher)之执行action

    调结者的执行action StrutsExecuteFilter类的工作就是执行对应的action请求.StrutsExecuteFilter类的工作还需要有一个叫ExecuteOperations类 ...