Comments (17)
谢谢师傅,可能我表达的有问题。 这个问题的关键不是直接通过查找css选择器来定位到跳转的url。为什么这么说,假设在一个网页css标签不固定的爬虫场景下,会有下面的问题:
- 如果这个加载href 标签在另一个css中该怎么办?
- 如果这里没有href,是ajax该怎么办?
比如:ajax.open('post',"ajax_link.php?id=1&t="+Math.random(),false); - 假如这里的css标签不固定,无法定位[id='1']等,该怎么办?
我整理的解决方案是渲染好这个网页,然后取出所有链接。可能我的表达不够清晰,附上两个链接。
https://studygolang.com/articles/28614?fr=sidebar
http://blog.fatezero.org/2018/04/09/web-scanner-crawler-02/
from rod.
我猜你是卡在 id 这里了,这个其实跟 rod 没有关系,id 是不可以为数字的,你的页面本身写的有问题。
css 选择器标准里 # 后面直接跟数字是非法的,比如直接在浏览器运行 document.querySelect("#123")
是会报错的。
func Example() {
browser := rod.New().Connect()
defer browser.Close()
page := browser.Page("http://demo.aisec.cn/demo/aisec/")
button := page.Element("[id='1']")
wait := page.WaitRequestIdle()
button.Click()
wait()
href := page.Element("#abc a").Eval(`() => this.href`).String()
fmt.Println(href)
// Output: http://demo.aisec.cn/demo/aisec/click_link.php?id=2
}
from rod.
其实 rod 的报错信息里应该就说了选择器非法,你可能没仔细看
from rod.
不知道怎么写选择器可以鼠标右键这个元素让 chrome 帮你生成:
from rod.
感谢师傅百忙抽空回复!
from rod.
抱歉,不是很理解你的表达,我回复的代码不是解决了你的问题吗?没有解决吗?
from rod.
爬虫之所以能爬一个页面的基本条件是什么?是这个页面能体现某种客观规律,我们叫这个 pattern (模式),如果页面完全随机没有任何规律那即使引入 AI 也是没法爬的。
大部分网页是动态的,你要做的事就是找到页面不变的模式。比如你说的无法定位只是你没逆向工程理解到它不变的模式,你总有方法定位它的,否则这个网页如此随机即普通用户根本就没法稳定使用。你想想一个 div 有时在右上角,有时在左下角,有时不出现,页面没有任何元素有 css class 或者属性,全是 plain div,没有规律可循,老板要求你得到这个 div 里的文字你觉得可能吗?他自己都不知道哪个才是他想要的 div。
from rod.
练习写爬虫最好是拿个实际的网站作为入口练手,这样你碰到的问题才好向他人解释,否则很难帮你,你可能提出一些业内难以解决的问题。我一开始学的时候也是想当然觉得很多事是有解决方案的,然而现实是满地都是 P/NP 问题,很多看起来很简单的问题根本就没法简单解决。
比如网站里会随机的出现 bug,这些都是开发这个页面的人自己都没考虑的的问题,你就必须要考虑清楚才能稳定的爬,请问有人可以有个算法能检查网站的 bug 吗?通常的检测工具只能狭隘的检查一些常见 bug,要不然怎么那么多程序员加班修 bug 呢?
from rod.
谢谢师傅,你回复的代码解决了问题。感谢师傅指导。顺便分享一个hook网页中所有url的项目,https://github.com/myvyang/chromium_for_spider/blob/master/README.zh-cn.md
from rod.
再次感谢师傅之前回复,我自己去多练习练习。感谢~
from rod.
https://github.com/myvyang/chromium_for_spider/blob/master/README.zh-cn.md
我觉得他写这个是没有理解到 chromium 本来就给了 api 来 hook 或 block 任意请求,完全没必要修改 chromium 源代码,文档见 https://chromedevtools.github.io/devtools-protocol/tot/Fetch/
不是很理解他写这个项目的意义。
from rod.
请问rod怎么调用这个api来hook全部请求呢?
from rod.
例子直接调用 cdp API:
这是截获一个下载的例子:
https://github.com/ysmood/rod/blob/b073d270d4ef3ad723dcc7dcbd18b6a194d95cf2/page.go#L201-L282
from rod.
IDE 里输入 proto.
自动补全你要调用的 API 的名字
from rod.
建议反复把所有的 example 好好理解下。每个 examples_test.go
文件里的例子都非常关键。
任何 go 项目的学习方法都是看 example 文件,然后度 unit test,如果你还有问题再来提问。
from rod.
要是都像你这样问,那开发者都没时间写代码了,光顾着回答问题 😂
from rod.
嗯嗯,谢谢师傅耐心解答。感谢感谢。我再去多看看案例,顺便加强英语,感谢~
from rod.
Related Issues (20)
- Add support for Linux Arm? for downloading etc. HOT 4
- Add an initEvents public function with a Browser structure HOT 5
- page.Navigate(url), the page is like blocked HOT 1
- 测试时如何获取当前网页 HOT 5
- Click Button Eval Js Not Working HOT 4
- 获取点击后的response失败 HOT 2
- How to hide chromium command prompt HOT 2
- Element存在,但Element.MustHas却为false,检测不到Element HOT 2
- How to open a new Chrome window while using remote control HOT 4
- Using go-rod with firefox, failing to start new instance of firefox HOT 6
- How to disable the dialog when open External protocols HOT 2
- How to Set or replace response headers HOT 2
- Proxy and Hijack HOT 3
- Just a recipe to run a go-rod program as systemd service HOT 1
- How to Log CDP Logs to a File or Use slog HOT 2
- Failed to deserialize params.body - BINDINGS: binary value expected at position 61 HOT 5
- failed page screenshot in defer HOT 2
- launcher docker 是否支持常驻且直接连接 HOT 4
- Examples fail on Ubuntu 24.04 with "No usable sandbox!" HOT 8
- Open the webpage, the page size is changed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rod.