使用 python 检索嵌入在另一个网页中的评论 (disqus)

Retrieving comments (disqus) embedded in another web page with python

我正在使用 python 3.5 (Beautifulsoup) 删除一个网站。我可以阅读源代码中的所有内容,但我一直试图从 disqus 中检索嵌入的评论但没有成功(这是对脚本的引用)。

html 源代码片段如下所示:

var disqus_identifier = "node/XXXXX";
script type='text/javascript' src='https://disqus.com/forums/siteweb/embed.js';

src 发送到脚本函数。

我已经阅读了 Whosebug 中使用 selenium 的建议,但我真的很难让它工作但没有成功。我知道 selenium 模拟浏览器(我认为这对我想要的东西来说太重了)。但是,我的网络驱动程序有问题,它无法正常工作。所以,我放弃了这个选项。

我希望能够执行脚本并检索带有注释的 .js。 我发现一个可能的解决方案是 PyV8。但是我无法在 python 中导入。 我阅读了互联网上的帖子,我用谷歌搜索了它,但它不起作用。

我安装了 Sublime Text 3 并在以下位置手动下载了 pyv8-win64-p3:

C:\Users\myusername\AppData\Roaming\Sublime 文字 3\Installed Packages\PyV8\pyv8-win64-p3

但我不断收到:

ImportError: No module named 'PyV8'.

如果有人能帮助我,我将非常感谢。

因此,您可以通过研究其网络流量来构建 Disqus API;在页面源中,所有必需的数据都存在。喜欢 Disqus API 发送一些查询字符串。最近从Disqus中提取评论API,这里是示例代码

示例: 这里汤-页面源和params_dict = json.loads(str(soup).split("embedVars = ")[1].split(";")[0])

def disqus(params_dict,soup):
    headers = {
    'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0'
    }
    comments_list = []
    base = 'default'
    s_o = 'default'
    version = '25916d2dd3d996eaedf6cdb92f03e7dd'
    f = params_dict['disqusShortname']
    t_i = params_dict['disqusIdentifier']
    t_u = params_dict['disqusUrl']
    t_e = params_dict['disqusTitle']
    t_d = soup.head.title.text
    t_t = params_dict['disqusTitle']
    url = 'http://disqus.com/embed/comments/?base=%s&version=%s&f=%s&t_i=%s&t_u=%s&t_e=%s&t_d=%s&t_t=%s&s_o=%s&l='%(base,version,f,t_i,t_u,t_e,t_d,t_t,s_o)
    comment_soup = getLink(url)
    temp_dict = json.loads(str(comment_soup).split("threadData\" type=\"text/json\">")[1].split("</script")[0])
    thread_id = temp_dict['response']['thread']['id']
    forumname = temp_dict['response']['thread']['forum']
    i = 1
    count = 0
    flag = True
    while flag is True:
        disqus_url = 'http://disqus.com/api/3.0/threads/listPostsThreaded?limit=100&thread='+thread_id+'&forum='+forumname+'&order=popular&cursor='+str(i)+':0:0'+'&api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F'
        comment_soup = getJson(disqus_url)

它将 return json 并且您可以在其中找到可以提取评论的评论。希望对您有所帮助。

对于 Facebook 嵌入的评论,您可以使用 Facebook 的图表 api 以 json 格式提取评论。

示例-

Facebook 评论 - https://graph.facebook.com/comments/?ids= "link of page"