Java从Tiktok获取URL

huangapple go评论108阅读模式
英文:

Java get URL from Tiktok

问题

try {
URL url = new URL("https://www.tiktok.com/@petyyyyyy?lang=de");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

  String inputLine;

  while((inputLine = in.readLine()) != null) {
      System.out.println(inputLine);
  }

} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}

英文:

I want to get the code of a website. In this case it's a tiktok page.

My code looks like this:

  try {
        URL url = new URL("https://www.tiktok.com/@petyyyyyy?lang=de");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;

        while((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }

    } catch (MalformedURLException me) {
        System.out.println(me);
    } catch (IOException ioe) {
        System.out.println(ioe);
    }

This works perfect for any normal website. The problem is that it doesnt' work with this tiktok site.

I think it's because there is an @ in the URL and Java has a problem with it.

Any help appreciated

答案1

得分: 1

你可以尝试类似这样的代码:

try {
    URL url = new URL("https://www.tiktok.com/%40petyyyyyy?lang=de");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty ("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0");
    
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
    }

} catch (MalformedURLException me) {
    System.out.println(me);
} catch (IOException ioe) {
    System.out.println(ioe);
}
英文:

You can try something like that:

try {
        URL url = new URL("https://www.tiktok.com/%40petyyyyyy?lang=de");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty ("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0");
        
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }

    } catch (MalformedURLException me) {
        System.out.println(me);
    } catch (IOException ioe) {
        System.out.println(ioe);
    }

答案2

得分: 0

好的,以下是您要翻译的内容:

似乎TikTok正在阻止对用户页面的请求。

解决方法:

在我的情况下,我只是想从网站上获取关注者人数。

我只需使用另一个网站,即tiktok关注者计数器

英文:

It looks like Tiktok is blocking requests to the user page.

Workaround:

In my case, I just wanted to get the follower amount from the site.

I simply use another website, i.e. tiktok follower counter

huangapple
  • 本文由 发表于 2020年8月22日 02:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定