[爬虫]请教各位高手,除了 Seleium 的方法,还有什么现成轮子方法可以获得渲染后的网页代码?

2019-07-25 00:35:06 +08:00
 qazwsxkevin

在学习和使用的过程中,我是使用 Seleium+chromedriver 去 driver.page_source 获得渲染后的 html 代码,不知道为什么,用 chromedriver 去 get 网页,webdriver 通常一个页面需要 1~2 分钟才能结束进程得到 html 代码,一两个页面没什么问题,现在批量,就很花时间(这个现象好像也不少人跟我一样的情况,我换电脑,换 chromedriver 版本,甚至换个城市换个运营商都是这样,笑~)
回到问题本身,我其实就是需要找其它什么轮子,可以去获取一个渲染后的页面 html ?(渲染就是鼠标把网页从顶部滚到底部,js 执行完了,网页就渲染好了,不需要其它什么操作)

#使用 Chrome 获取网页代码成 Str
def GetChromeLinkSourcetoStr(link):
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('blink-settings=imagesEnabled=false')
    driver = webdriver.Chrome(options=options)
    wait = WebDriverWait(driver, 10)
    driver.maximize_window()
    driver.get(link)
    tmpStr = driver.page_source
    return tmpStr
5225 次点击
所在节点    Python
29 条回复
sivacohan
2019-07-25 00:48:36 +08:00
加上这个试试。
options.add_argument('--no-proxy-server')
yh7gdiaYW
2019-07-25 01:05:20 +08:00
Puppeteer
Rorshach
2019-07-25 01:31:37 +08:00
可以用 splash,这个是异步的,比 selenium 快吧,还可以试试 Phantom JS 这种无界面浏览器
不过如果每个页面要 1~2 分钟才能得到代码,应该不是框架的问题吧……
shuangyeying
2019-07-25 07:27:40 +08:00
你去看看要爬的网页有没有引用墙外的资源,如果有的话,可能这就是加载慢的原因了。另外可以装上广告屏蔽插件再试试。
Leigg
2019-07-25 07:51:29 +08:00
可以改浏览器参数,只是你不会。
get 方法默认是阻塞到浏览器加载完才向下执行,如果加载失败这个窗口也用不了。但是有可能浏览器已经加载出想要的内容,还是没停下,这时候想要操作浏览器就必须改个参数。晚点发上来
nullboy
2019-07-25 08:05:13 +08:00
Puppeteer。但是真心不会写 node,现在是大部分工作是 python+selenium 实现的,部分限制 selenium 的功能用 puppeteer
lzxz1234
2019-07-25 08:46:31 +08:00
如果是 python 可以考虑下 pyspider
lozzow
2019-07-25 08:50:26 +08:00
@nullboy 我记得我个日本老哥写了一个 py 版本的
Jaho
2019-07-25 08:51:29 +08:00
python 有 pyppeteer 啊 为啥你们都说都推荐去写 node 至于速度问题 不至于要 1-2 分钟吧
附: http://bb.jaho.fun/Pyppeteer.html
防踩坑
sep1025
2019-07-25 08:56:06 +08:00
requests-html,集成 pyppeteer
foxyier
2019-07-25 09:18:18 +08:00
selenium,设置不加载图片之类的东西,应该很快的
momocraft
2019-07-25 09:22:33 +08:00
selenium 是不是停好久了
puppeteer 挺香的
ClericPy
2019-07-25 09:34:47 +08:00
有 selenium 写爬虫从始至终都是很傻的行为吧, phantomjs 作者放弃开发了, selenium 还活着

楼主直接用 chrome 远程调试那个 cdp 协议吧, chrome devtools protocol
简单的用 http/socket 请求就可以操作 chrome, 用 js 实现不断翻页, 用 cdp 里的 Network 截取流量判断是否最后一页

你不给个例子, 也不好给你看看示例代码
kidlfy
2019-07-25 09:38:22 +08:00
还是 puppeteer 吧 是 chrome 团队维护的靠谱点
tohearts
2019-07-25 09:48:31 +08:00
我使用的是 splash, 异步配异步
fank99
2019-07-25 10:05:30 +08:00
splash
momocraft
2019-07-25 10:35:34 +08:00
如果你实在不想写 node,找人写个 puppeteer 打开网页返回 html 的服务也没几行

不过里面出错你无法 debug
shawndev
2019-07-25 10:37:05 +08:00
puppeteer
Vegetable
2019-07-25 11:00:35 +08:00
你先浏览器正常加载一下看看多久能加载完好吗
Leigg
2019-07-25 14:54:35 +08:00
@Leigg
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.CHROME # 修改页面加载策略
# none 表示将 br.get 方法改为非阻塞模式,在页面加载过程中也可以给 br 发送指令,如获取 url,pagesource 等资源。
desired_capabilities["pageLoadStrategy"] = "none"


driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=chrome_driver_path,
desired_capabilities=desired_capabilities)
拿去吧。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/585954

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX