【问题标题】:Extracting comments from news articles从新闻文章中提取评论
【发布时间】:2013-09-25 06:08:37
【问题描述】:

我的问题与此处提出的问题类似: https://stackoverflow.com/questions/14599485/news-website-comment-analysis 我正在尝试从任何新闻文章中提取 cmets。例如。我在这里有一个新闻网址: http://www.cnn.com/2013/09/24/politics/un-obama-foreign-policy/ 我正在尝试在 python 中使用 BeautifulSoup 来提取 cmets。但是,评论部分似乎嵌入在 iframe 中或通过 javascript 加载。通过firebug查看源码并不能揭示cmets部分的源码。但是通过浏览器的查看源功能显式查看 cmets 的源代码。如何提取 cmets,尤其是当 cmets 来自嵌入在新闻网页中的不同 url 时?

这是我到目前为止所做的,虽然这并不多:

    import urllib2
    from bs4 import BeautifulSoup

    opener = urllib2.build_opener()


    url = ('http://www.cnn.com/2013/08/28/health/stem-cell-brain/index.html')


urlContent = opener.open(url).read()
soup = BeautifulSoup(urlContent)
title = soup.title.text

print title
body = soup.findAll('body')
outfile = open("brain.txt","w+")
for i in body:
    i=i.text.encode('ascii','ignore')
    outfile.write(i +'\n')

对于我需要做什么或如何去做的任何帮助将不胜感激。

【问题讨论】:

  • 你还需要尝试 Selenium 之类的东西来模拟浏览器的 javascript 功能。
  • @SnakesandCoffee 对于特定情况,您不需要 js。它只是一个 iframe,您可以下载整个页面。

标签: python comments web-scraping beautifulsoup


【解决方案1】:

它在iframe 中。检查带有id="dsq2" 的帧。

现在iframe 有一个src attr,它是指向具有cmets 的actual site 的链接。

so in beautiful soup: css_soup.select("#dsq2") 并从 src 属性中获取 url。它会引导你到一个只有 cmets 的页面。

要获取实际的 cmets,从 src 获取页面后,您可以使用这个 css 选择器:.post-message p

如果你想加载更多评论,当你点击更多 cmets 按钮时,它似乎正在发送:

http://disqus.com/api/3.0/threads/listPostsThreaded?limit=50&thread=1660715220&forum=cnn&order=popular&cursor=2%3A0%3A0&api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多