阅读:4066回复:24
tampermonkey 如何获取 这个网址的的文章正文内容
像起点的很简单,例如这个页面 https://read.qidian.com/chapter/YpTCe7ZNThACpOPIBxLXdQ2/83iDwee_bG6aGfXRMrUjdw2/
我用 bookname = $("#bookImg").text(); chapname = $(".content-wrap").text(); chaptext = document.querySelector(".read-content"); 就能获取 书名 章节名称 正文内容 但是下面这个网址的不知道如何获取 https://chuangshi.qq.com/bk/xh/AGwEMl1nVjYAOFRtATMBZg-r-1.html 我只会获取 书名 let test2 = $(".bookNav").contents().eq(6).text(); 谁帮我写个代码 获取章节名称 和 正文内容,谢谢 |
|
1楼#
发布于:2024-04-16 00:23
无论如何感谢帮忙,我太笨后续一些还是搞不定,先这样,我有空再折腾
|
|
2楼#
发布于:2024-04-15 23:53
|
|
3楼#
发布于:2024-04-15 23:23
我猜是需要加入代码等待载入完成,可是我用百度搜到的没成功...
不过,我又测试了您说的第二种办法,成功取得了章节列表 现在就是还存在我提问的第一个问题,这个链接怎么获取 1.如何获取 点击右侧作者名字 或 作者头像 跳转到作者所有作品的链接,就是如何获取这个地址https://home.zongheng.com/people/19335221 |
|
4楼#
发布于:2024-04-15 22:46
taoww:我试了下,是因为tampermonkey实现了个沙盒,你这样写没法取得页面环境中window的属性加上// @grant none确实不报错了,但是console.log(a.textContent)什么也没输出啊,只能输出我加的'test' // ==UserScript== // @name why // @version 0.1 // @description books. // @match https://www.zongheng.com/detail/* // @require https://code.jquery.com/jquery-3.6.1.min.js // @grant none // ==/UserScript== /* globals jQuery, $, waitForKeyElements */ (function() { 'use strict'; console.log('test'); open("https://home.zongheng.com/people/" + window.__NUXT__.state.detail.detailBook.authorInfo.authorId, "_blank"); document.querySelectorAll('.detail-catalog--chapter .chapter-list--item-main').forEach((a)=>{ console.log(a.textContent) }); console.log('test2'); })(); |
|
5楼#
发布于:2024-04-15 20:21
fanta:// ==UserScript==我试了下,是因为tampermonkey实现了个沙盒,你这样写没法取得页面环境中window的属性 在// ==/UserScript==之前加一行 // @grant none禁止掉沙盒就能正常工作了。 或者加上 // @grant unsafeWindow再把代码中的window改成unsafeWindow,可以保留沙盒 |
|
6楼#
发布于:2024-04-15 18:38
taoww:首先去查看页面源代码,搜索看有没有包含 window.__NUXT__= 这么一段脚本的script。firefox 报错是Uncaught (in promise) TypeError: window.__NUXT__ is undefined safari报错是Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'window.__NUXT__.state') |
|
7楼#
发布于:2024-04-15 18:32
taoww:首先去查看页面源代码,搜索看有没有包含 window.__NUXT__= 这么一段脚本的script。 // ==UserScript== // @name why // @version 0.1 // @description books. // @match https://www.zongheng.com/detail/* // @require https://code.jquery.com/jquery-3.6.1.min.js // ==/UserScript== /* globals jQuery, $, waitForKeyElements */ (function() { 'use strict'; console.log('test'); open("https://home.zongheng.com/people/" + window.__NUXT__.state.detail.detailBook.authorInfo.authorId, "_blank"); document.querySelectorAll('.detail-catalog--chapter .chapter-list--item-main').forEach((a)=>{ console.log(a.textContent) }); })(); 把@run-at设置成了document-start ..没用过..嘿嘿... 搜索window.__NUXT__是有的 window.__NUXT__ = (function(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D) { |
|
8楼#
发布于:2024-04-15 17:44
|
|
9楼#
发布于:2024-04-15 16:26
|
|
10楼#
发布于:2024-04-15 10:15
fanta:hello,大神,好久不见,我又来向您求助了 看不懂html代码就去学啊,十多年前我也是用firebug边看html结构边学jquery的。现在自带的F12开发工具比当年的firebug强大多了,一年过去了怎么这点长进都没有 open("https://home.zongheng.com/people/" + window.__NUXT__.state.detail.detailBook.authorInfo.authorId, "_blank"); document.querySelectorAll('.detail-catalog--chapter .chapter-list--item-main').forEach((a)=>{ console.log(a.textContent) });或者直接用它的api了,不跟html纠缠,就不用等它把目录加载完毕 fetch("https://bookapi.zongheng.com/api/chapter/getChapterList", { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: "bookId=" + window.__NUXT__.state.detail.detailBook.book.bookId, }).then(async (data)=>{ let response = await data.json(); if (response.code == 0) { for (const tome of response.result.chapterList) { for (const chap of tome.chapterViewList) { console.log(tome.tome.tomeName + "-" + chap.chapterName) } } } }); |
|
11楼#
发布于:2024-04-05 13:32
taoww:$("#txt")这个一开始就存在的话,你就换个判定方式啊,比如用hello,大神,好久不见,我又来向您求助了 帮忙看下这个链接, https://www.zongheng.com/detail/1203325?tabsName=catalogue 1.如何获取 点击右侧作者名字 或 作者头像 跳转到作者所有作品的链接,就是如何获取这个地址https://home.zongheng.com/people/19335221 2.如何获取 所有章节列表 其他小网站简单用 chaplist = $('#list').html();就行 这个复杂网站用 chaplist = $('.detail-catalog--chapter').html(); 不行,完全看不懂代码 |
|
12楼#
发布于:2023-08-09 13:57
|
|
13楼#
发布于:2023-08-09 11:27
$("#txt")这个一开始就存在的话,你就换个判定方式啊,比如用
$("#txt").text() == "" 检查$("#txt")里面有没有文本,或者用 $("#txt").children().length == 0检查$("#txt")里面有没有其他非文本元素存在 |
|
14楼#
发布于:2023-08-07 15:10
taoww:抱歉,我上面把promise那套写错了。正确的是要把同步执行的放到同一个async函数里,我这里就把最外层的换了,差不多像这样hello,高手,我又来咯 上次这个代码非常好使 // ==UserScript== // @name testok // @version 0.1 // @description test // @match https://chuangshi.qq.com/* // @require https://code.jquery.com/jquery-3.6.1.min.js // ==/UserScript== /* globals jQuery, $, waitForKeyElements */ (async function() { 'use strict'; let chaptext; console.log($(".bookreadercontent").length);//没问题 0 await new Promise((resolve,reject)=>{ (function x(){ if ($(".bookreadercontent").length == 0) { setTimeout(x, 100); return; } resolve(); })(); }); console.log($(".bookreadercontent").length);//没问题 1 chaptext = document.querySelector(".bookreadercontent"); document.body.innerHTML = ` <div class="chaptext">${chaptext.innerHTML}</div> `; })();然后另一个网站suiyuexs.com的随意一个页面例如: https://www.suiyuexs.com/read/114583/23686286.html这个网站只要是服务器稍微慢点的情况下,就会 获取不到, 所以我我照猫画虎改了下,没成功, $("#txt").length没有载入的时候就==1了... 还是您帮我改改吧,hehe,下面是不行的代码 // ==UserScript== // @name test // @version 0.1 // @description test // @match https://www.suiyuexs.com/read/* // @require https://code.jquery.com/jquery-3.6.1.min.js // ==/UserScript== /* globals jQuery, $, waitForKeyElements */ (async function() { 'use strict'; let chaptext; console.log($("#txt").length);//有问题 即使没有加载成功也=1 await new Promise((resolve,reject)=>{ (function x(){ if ($("#txt").length == 0) { setTimeout(x, 100); return; } resolve(); })(); }); console.log($("#txt").length);// chaptext = document.querySelector("#txt"); document.body.innerHTML = ` <div class="chaptext">${chaptext.innerHTML}</div> `; })(); |
|
上一页
下一页