栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

爬虫代码范例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫代码范例

JAVA爬虫(爬取东方财富网)
pom.xml


    4.0.0

    org.example
    Spider
    1.0-SNAPSHOT
    
        
            net.sourceforge.htmlunit
            htmlunit
            2.49.1
        
        
            com.alibaba
            fastjson
            1.2.76
        
        
            commons-io
            commons-io
            2.8.0
        
    
    
        8
        8
    

代码源码
package spider;

import com.alibaba.fastjson.JSONObject;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class Spider_List {
    public static void main(String[] args) throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        String url = "http://26.push2.eastmoney.com/api/qt/clist/get?" +
                "pn=1" +
                "&pz=5000" +
                "&po=0" +
                "&np=1" +
                "&fltt=2" +
                "&invt=2" +
                "&fid=f12" +
                "&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23" +
                "&fields=f12,f13";
        String json = webClient.getPage(url).getWebResponse().getContentAsString();
        Object[] objects = JSONObject.parseObject(json).getJSONObject("data").getJSONArray("diff").toArray();
        for (Object object : objects) {
            String F12 = JSONObject.parseObject(object.toString()).getString("f12");
            String F13 = JSONObject.parseObject(object.toString()).getString("f13");
            String url1 = "http://12.push2his.eastmoney.com/api/qt/stock/kline/get?secid=" + F13 + "." + F12 + "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=60&fqt=0&end=20500101&lmt=120";
            String json2 = webClient.getPage(url1).getWebResponse().getContentAsString();
            System.out.println(json2);
            FileUtils.writeStringToFile(new File("C:\Users\LN\Desktop\data_60.csv"), json2 + "n", "UTF-8", true);
        }
        System.out.println("总共" + objects.length + "条数据");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293003.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号