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

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. calico性能测试

    硬件环境: 三台虚拟机: 192.168.99.129 master(kube-apiserver.kube-controller-manager.kube-proxy.kube-scheduler. ...

  2. PHP-preg_replace过滤字符串代码

    $str=preg_replace("/\s+/", " ", $str); //过滤多余回车       $str=preg_replace("/& ...

  3. Data Collection

    众所周知,计算机领域论文是要以实验为基础的,而实验的原料就是数据.不管是在图像,文字或者语音领域,开源的数据都十分宝贵和重要.这里主要收集各领域的一些常用的公开数据集. 计算机视觉: [ImageNe ...

  4. mui 子页面切换父页面底部导航

    在父页面中新增方法: function switchTab(tab){ plus.webview.hide(activeTab); activeTab= tab; plus.webview.show( ...

  5. iOS -转载-使用Navicat查看数据表的ER关系图

    Navicat软件真是一个好东西.今天需要分析一个数据库,然后想看看各个表之间的关系,所以需要查看表与表之间的关系图,专业术语叫做ER关系图. 默认情况下,Navicat显示的界面是这样的: 软件将表 ...

  6. 在odl中怎样实现rpc

    opendaylight作为sdn主要开源项目,採用osgi框架.已经得到非常多厂商的支持.氦版本号也公布在即. 以下介绍一下在odl中怎样实现rpc. odl使用yang作为model模型定义文件. ...

  7. RocketMQ性能压测分析(转载)

    一   机器部署 1.1  机器组成 1台nameserver 1台broker  异步刷盘 2台producer 2台consumer   1.2  硬件配置 CPU  两颗x86_64cpu,每颗 ...

  8. 让python pip使用国内镜像

    国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...

  9. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  10. c#的小技巧

    很多.net的使用小技巧,总是要自己记下来的,给自己. 一:时间格式话中H和h的区别 DateTime.ToString("yyyy-MM-dd HH:mm:ss");//转化成2 ...