半肾
精华
|
战斗力 鹅
|
回帖 0
注册时间 2016-12-11
|
本帖最后由 三葉Mitsuha 于 2020-1-28 22:44 编辑
写过一个JS脚本,每次运行只能爬一个帖子,最多支持999页。
这个脚本是通过调用discuz的api,并把api返回的数据独立保存成一个.json文件(只能保存文本内容,非文本的无能为力了)
- (function(THREAD_ID, MAX_PAGE) {
- let pageIndex = 0
- const downloadContent = function(contentText) {}
- const rename = num => num > 99 ? num : num > 9 ? '0' + num : '00' + num
- const getJSON = async function() {
- ++pageIndex;
- if (pageIndex > MAX_PAGE) {
- console.log('END')
- return
- }
- try {
- const xhr = new XMLHttpRequest()
- xhr.open('GET', `/2b/api/mobile/index.php?version=4&module=viewthread&tid=${THREAD_ID}&page=${pageIndex}`)
- xhr.overrideMimeType('text/plain; utf-8');
- xhr.setRequestHeader('Content-type', "text/plain; charset=utf-8")
- xhr.onreadystatechange = () => {
- if (xhr.readyState !== 4) {
- return
- }
- const blob = new Blob([JSON.stringify(JSON.parse(xhr.responseText))], {type : 'application/json'})
- const anchor = document.createElement('a')
- const fileName = `thread_${THREAD_ID}_${rename(pageIndex)}.json`
- anchor.download = fileName
- anchor.href = window.URL.createObjectURL(blob)
- anchor.click()
- window.URL.revokeObjectURL(anchor.link)
- setTimeout(getJSON, 200)
- }
- xhr.send()
- } catch (err) {
- console.error(err)
- }
- }
- getJSON()
- })()
复制代码
使用方法,以chrome为例
0、进入你要爬的discuz的网页
1、打开浏览器开发者工具(f11?)
2、切至console
3、把以上代码粘贴至console,并在最末尾的括号内填写两个数字参数,用英文逗号隔开,之后敲击回车执行代码。
4、第一个参数是帖子id(通过网址可以得到),第二个参数是最大页码数。
5、第一次运行的话,chrome应该会询问你是否要下载多个文件,点击允许即可。
如果实在来不及找别的工具,可以先用这段代码把文本内容全弄下来。 |
评分
-
查看全部评分
|