• HttpClient抓取数据出现java.net.URISyntaxException: Expected authority at index 8: https://


    最近在使用HttpClient抓取京东的数据时。爬取爬到一半突然出现一个错误。如下:

    查找了一些网上资料,说地址中涉及了特殊字符,如‘|’‘&’等。所以不能直接用String代替URI来访问。必须采用%0xXX方式来替代特殊字符。但这种办法不直观。所以只能先把String转成URL,再能过URL生成URI的方法来解决问题。代码如下:

    URI uri = null;
            try {
                URL url2 = new URL(strUrl);
                uri = new URI(url2.getProtocol(), url2.getHost(), url2.getPath(), url2.getQuery(), null);
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
    // 创建httpGet请求对象,设置url地址
    HttpGet httpGet = new HttpGet(uri);
     

    即可解决这个问题

  • 相关阅读:
    【PKUWC2018】Minimax
    Codeforces Round #673
    【BZOJ1901】Dynamic Rankings
    【笔记】树状数组
    【CF1420E】Battle Lemmings
    Codeforces Round #672
    Problem A: STL——灵活的线性表
    algorithm
    iterator
    TRY vector
  • 原文地址:https://www.cnblogs.com/lpss-75074038/p/13710237.html
Copyright © 2020-2023  润新知