8-使用爬虫登录后台系统并爬取数据
来源:互联网 发布:手机信号干扰软件 编辑:程序博客网 时间:2024/05/21 16:23
使用爬虫登陆后台系统并爬取数据
package cn.itcast.spider.login;import java.util.ArrayList;import org.apache.http.Header;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;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 Login { public static void main(String[] args) throws Exception { // 1、创建httpClient CloseableHttpClient httpclient = HttpClients.createDefault(); // 2、创建Post对象 HttpPost httpPost = new HttpPost("http://shop.itcast.cn/login/login.html"); // 2.1 设置参数 // url // username password reURL ArrayList<BasicNameValuePair> paramList = new ArrayList<BasicNameValuePair>(); paramList.add(new BasicNameValuePair("username", "itcast")); paramList.add(new BasicNameValuePair("password", "itcast")); paramList.add(new BasicNameValuePair("reURL", "http://shop.itcast.cn/item/itemList.html")); // 2.2 将参数设置到httpPost对象 httpPost.setEntity(new UrlEncodedFormEntity(paramList)); // 3、执行post请求 CloseableHttpResponse response = httpclient.execute(httpPost); // 4、获取返回状态 int code = response.getStatusLine().getStatusCode(); System.out.println(code); if (200 == code) { System.out.println("login successful"); } // 发现页面重定向了(已经登录成功) if (302 == code) { Header[] locations = response.getHeaders("Location"); System.out.println(locations[0].getValue()); // 获得登录后跳转的url,直接发起httpget请求 CloseableHttpClient hc = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(locations[0].getValue()); CloseableHttpResponse res = hc.execute(httpGet); if (res.getStatusLine().getStatusCode() == 200) { String html = EntityUtils.toString(res.getEntity()); System.out.println(html); // 使用Jsoup解析 Document doc = Jsoup.parse(html); // 获取商品所在div Elements trs = doc.select("table tr"); ArrayList<Product> arrayList = new ArrayList<Product>(); for (Element element : trs) { Product product = new Product(); Elements inputs = element.select("input[name^=itemList]"); for (Element element2 : inputs) { String name = element2.attr("name"); String value = element2.attr("value"); if (name.contains("name")) { product.setName(value); } else if (name.contains("price")) { product.setPrice(value); } else if (name.contains("createTime")) { product.setDate(value); } else if (name.contains("detail")) { product.setDes(value); } } if (product.getName() != null) { arrayList.add(product); } } System.out.println(arrayList); } } }}
阅读全文
1 0
- 8-使用爬虫登录后台系统并爬取数据
- php简单爬虫使用登录账号密码爬取列表(爬虫二篇)
- 爬虫三部曲之(三):Python模拟登录云笔记网站并爬取笔记内容
- 爬虫实战爬取数据
- Python 使用BeautifulSoup方式爬虫爬取数据
- [python爬虫] 招聘信息定时系统 (一).BeautifulSoup爬取信息并存储MySQL
- python爬虫由浅入深9---定向爬取股票数据信息并保存至本地文件
- Python爬虫-利用百度地图API接口爬取数据并保存至MySQL数据库
- 登录教务爬取数据
- php后台“爬虫”模拟登录第三方系统(三)---模拟登录学校正方系统
- 如何使用laravel搭建后台登录系统
- 如何使用laravel搭建后台登录系统
- 使用画布做后台登录系统背景
- [Python]使用Scrapy爬虫框架简单爬取图片并保存本地
- scrapy爬虫实战(四)--------------登陆51job并使用cookies进行爬取
- 使用WebController爬虫框架进行微信公众号文章爬取并持久化
- PHP爬虫 爬取污染数据实例
- Scrapy 爬虫框架爬取网页数据
- 模版模式的规则
- linux----vi编辑器删除所有内容命令、ping网站指定数据包多少
- 【2017.11.28】1. Two Sum
- 震惊!学了三年计算机,不晓得void 和 void* 区别!
- 安卓透明度对应的16进制
- 8-使用爬虫登录后台系统并爬取数据
- 静态与动态RNN函数比较
- Application.StartupPath
- 企业中常用的RAID数据盘简介
- webpack插件-html-webpack-plugin
- INSERT INTO SELECT 实例
- 链式队列实现迷宫寻径
- 沉浸式透明渐变
- sdnu1521手写堆排