【发布时间】:2016-12-01 03:24:28
【问题描述】:
我正在编写一个网络爬虫。我提取了这个link 的标题和主要讨论,但我找不到任何评论(Ctrl+u -> Ctrl+f。评论文本)。我认为 cmets 是用 JavaScript 编写的。我可以提取它吗?
【问题讨论】:
标签: javascript python beautifulsoup web-crawler
我正在编写一个网络爬虫。我提取了这个link 的标题和主要讨论,但我找不到任何评论(Ctrl+u -> Ctrl+f。评论文本)。我认为 cmets 是用 JavaScript 编写的。我可以提取它吗?
【问题讨论】:
标签: javascript python beautifulsoup web-crawler
【讨论】:
document,"//www.spot.im/launcher/bundle.js");,但文件在哪里?我可以进入该文件以提取 cmets 吗?
RT 正在为 cmets 使用来自 spot.im 的服务
你需要做两个 POST 请求,首先https://api.spot.im/me/network-token/spotim 获取令牌,然后https://api.spot.im/conversation-read/spot/sp_6phY2k0C/post/353493/get 获取 JSON 格式的 cmets。
为此我编写了一个快速脚本
import requests
import re
import json
def get_rt_comments(article_url):
spotim_spotId = 'sp_6phY2k0C' # spotim id for RT
post_id = re.search('([0-9]+)', article_url).group(0)
r1 = requests.post('https://api.spot.im/me/network-token/spotim').json()
spotim_token = r1['token']
payload = {
"count": 25, #number of comments to fetch
"sort_by":"best",
"cursor":{"offset":0,"comments_read":0},
"host_url": article_url,
"canonical_url": article_url
}
r2_url ='https://api.spot.im/conversation-read/spot/' + spotim_spotId + '/post/'+ post_id +'/get'
r2 = requests.post(r2_url, data=json.dumps(payload), headers={'X-Spotim-Token': spotim_token , "Content-Type": "application/json"})
return r2.json()
if __name__ == '__main__':
url = 'https://www.rt.com/usa/353493-clinton-speech-affairs-silence/'
comments = get_rt_comments(url)
print(comments)
【讨论】:
comment.text 不是一种方法。 2. 我想从http://www.aljazeera.com/ 做同样的事情(评论提取)。我读到了 phantomjs 和 phantompy。我可以将它们用于此目的吗?
[comment['content'][0]['text'] for comment in comments['comments']]。如果要从多个站点提取数据,则需要为每个站点编写一个单独的提取器,而您可以使用 phantomjs(或任何其他无头浏览器),查找正在发出的 Web 请求将更加有效和高效浏览器并复制这些。就 aljazera 而言,每篇文章都有一个用于 cmets 的 RSS 提要。