在 JavaScript 执行后在 python 中加载网页 [英] Load web page in python AFTER JavaScripts executes

查看:19
本文介绍了在 JavaScript 执行后在 python 中加载网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户输入的内容获得西班牙语单词的定义(如字典).这个想法是:

<预><代码>>>>你好'1.中间U. como salutación 熟悉.

我第一次尝试使用 urllib2,但是由于定义是在 JS 执行之后出现的(有道理,废话)它没有用.我也试过硒,但据我所知,它必须打开一个导航窗口,对吗?我需要它像 urllib2 一样,不可见.

如果你想试试,我搜索定义的页面是http://lema.rae.es/drae/?val=word 其中 word 是用户输入的单词.

有什么想法吗?

解决方案

我可能会像 alecxe 建议那样做,但是我会使用加载定义本身的 URL.例如,搜索 azul:

from selenium import webdriver驱动程序 = webdriver.PhantomJS()driver.get('http://lema.rae.es/drae/srv/search?val=azul')打印 driver.find_element_by_css_selector("body>div").text

出现在问题中的 URL 加载一个页面,然后在 iframe 元素中加载定义的 URL.直接使用我上面显示的 URL 加载定义可以节省一些工作和一些复杂性:整个定义包含在 body 的第一个 div 子元素中.不幸的是,它并没有消除对 JavaScript 的需求.

运行上面的代码产生:

蔚蓝.(Quizá alterac. del ár. hisp. lazawárd, este del ár. lāzaward, este del persa laǧvard o lažvard, y este del sánscr. rājāvarta, rizo del rey).1. adj.Del color del cielo sin nubes.Es el quinto color del espectro 太阳能.美国C.s.2.米El cielo,el espacio.美国恩冷.诗人.3.米墨西哥.Miembro delcuerpo de Policía.~德钴.[... 等等 ...]

请注意,我没有检测到需要使用任何等待机制来检测页面内容是否准备就绪.在调试器中查看页面 a) 我没有看到任何 Ajax 请求和 b) 查看 JavaScript 和页面本身,看起来提供的是一个混淆页面,然后 JavaScript 同步反混淆.所以当 driver.get 返回时,内容应该可以使用了.

I am trting to get the definition of words in spanish (like a dictionary) based on what the user inputs. The idea would be:

>>> hola
'1. interj. U. como salutación familiar.'

I first tried with urllib2, but since the definition appeared after the execution of JS (makes sense duh) it didn't work. I also tried selenium, but from what I understood it has to open a navigator window, right? I need it to be like urllib2, invisible.

If you want to try, the page where I search the definition is http://lema.rae.es/drae/?val=word where word is the word the user inputs.

Any thoughts, anyone?

解决方案

I might do it like alecxe suggested, but I'd use the URL that loads the definition itself. For instance, searching for azul:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('http://lema.rae.es/drae/srv/search?val=azul')
print driver.find_element_by_css_selector("body>div").text

The URL that appears in the question loads a page that then loads the definition's URL in an iframe element. Loading the definition directly with the URL I show above saves some work and some complexity: the entire definition is contained in the first div child of body. Unfortunately, it does not remove the need for JavaScript.

Running the code above produces:

azul.
(Quizá alterac. del ár. hisp. lazawárd, este del ár. lāzaward, este del persa laǧvard o lažvard, y este del sánscr. rājāvarta, rizo del rey).
1. adj. Del color del cielo sin nubes. Es el quinto color del espectro solar. U. t. c. s.
2. m. El cielo, el espacio. U. m. en leng. poét.
3. m. Méx. Miembro del cuerpo de Policía.
~ de cobalto.
[... etc ...]

Note that I've not detected the need to use any wait mechanism to detect that the content of the page is ready. Looking at the page in a debugger a) I did not see any Ajax request and b) looking at the JavaScript and the page itself, it looks like what is served is an obfuscated page that the JavaScript then deobfuscates synchronously. So by the time driver.get returns, the content should be ready to be used.

这篇关于在 JavaScript 执行后在 python 中加载网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆