java - 从 Java 读取 Twitter 页面

标签 java url twitter urlconnection

我想通过使用该推文的公共(public)链接从 Java 读取该推文的内容,例如:

http://twitter.com/FoodRhythms/status/461201880354271232/photo/1

我使用与从其他类型的页面读取内容相同的过程:

String XMLstring = "";
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
    XMLstring += inputLine;
in.close();

但是,虽然对于其他页面来说这是可行的,但从 Twitter 链接读取时返回的内容为空(BufferedReader 对象不包含任何行)。

对此有任何提示吗?

最佳答案

您正在以不必要的复杂方式解决问题。您将使用 HTML 解析器库,例如 Jsoup解析其内容的 URL。

示例如下:

String url = "https://twitter.com/FoodRhythms/status/461201880354271232/photo/1";
Document doc = Jsoup.connect(url).get();
Element tweetText = doc.select("p.js-tweet-text.tweet-text").first();
System.out.println(tweetText.text());

它将输出

Miso-Glazed Japanese Eggplant 

以类似的方式,您可以选择任何您想要的元素!

关于java - 从 Java 读取 Twitter 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24997789/

相关文章:

Point 类的 Java hashCode

ios - 推特登录swift 4

python - 将推文分类为多个类别(无监督数据/推文)

java - JTextPane 背景颜色

java - 使用 KSOAP2 序列化要发送的整数数组

ios - 使用带有非法字符的 URL 的 NSURLConnection

ios - 如何在 iOS 的特定页面上打开 facebook 和 twitter

php - 关于Codeigniter url路由的问题

curl - 使用 Curl 和 Windows 命令行将照片发布到 Twitter

Java 8 Stream distinct 不起作用