java网络编程-爬虫+模拟浏览器

网络爬虫+模拟浏览器(获取有权限网站资源):
获取URL
下载资源
分析
处理

创新互联主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务淄博,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

public class http {

public static void main(String[]args) throws  Exception
{
    //http+s更安全
    //URL.openStream()打开于URL的连接,并返回一个InputStream用于从连接中读取数据
    //获取URL
    URL url=new URL("https://www.jd.com");
    //下载资源
    InputStream is = url.openStream();
    BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));;
    String msg=null;
    while((msg=br.readLine())!=null)
    {

        System.out.println(msg);
    }
    br.close();
}
}

获取有权限网络资源:

public class http {

public static void main(String[]args) throws  Exception
{
    //.openConnectio,,返回一个URLConnection实例表示由所引用的远程对象的连接URL
    //URLConnection的子类有HttpURLConnection和JarURLConnection
    URL url=new URL("https://www.jd.com");
    //下载资源
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setRequestMethod("GET");//模拟浏览器得get请求
    conn.setRequestProperty( "User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763");
    BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
    String msg=null;
    while((msg=br.readLine())!=null)
    {

        System.out.println(msg);
    }
    br.close();
}
}

分享题目:java网络编程-爬虫+模拟浏览器
本文链接:http://csdahua.cn/article/igogds.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流