【问题标题】:Fetching web page, but need javascript to view page content正在获取网页,但需要 javascript 才能查看页面内容
【发布时间】:2017-11-17 15:14:49
【问题描述】:

从网站获取数据/表单,我尝试了mechanize and selenium,都失败了。

机械化

脚本如下所示,

import sys
import mechanize
url ='xxx'
response2=br.open(url)
request = br.request
print (response2.info())
print (response2.read())

输出:

Cache-Control: no-store, must-revalidate, no-cache, max-age=0
Content-Type: text/html
Connection: close
Vary: Accept-Encoding
Pragma: no-cache
Expires: -1
CacheControl: no-cache
X-UA-Compatible: IE=edge
Content-Type: text/html; charset=utf-8

... more content ...

<noscript>Please enable JavaScript to view the page content.</noscript>
</head><body>
</body></html>

所以我想也许我可以用 selenium 来运行 js,比如

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
url= 'xxx'
driver.get(url)

print driver.context
print driver.title

print driver.page_source
driver.close()

但我又失败了,结果几乎一样:

...
<noscript>Please enable JavaScript to view the page content.</noscript>
...

我只想从站点中获取正确的内容/表单,并将 submitpost 的数据/表单发送到服务器以模拟 Web 浏览器访问操作。

我现在没有想法,我不太了解 selenium 是如何工作的,等待您的帮助,在此先感谢。

【问题讨论】:

  • 对不起,忘记网址了,网址是https://onlineservices.immigration.govt.nz/?WHS
  • 你可以尝试添加 this..profile = webdriver.FirefoxProfile()..profile.set_preference("javascript.enabled", True..broswer = webdriver.Firefox(profile)
  • 当我访问该页面时,他们会向我显示一个图像代码以防止非人类访问者。显然,他们不希望您获取该数据。
  • @codeiscool 也有人找到了图片代码,但我没看到。我不知道为什么。也许他们有用于图像代码的 IP 过滤器。

标签: javascript python selenium mechanize


【解决方案1】:

试试这个:
使用以下配置文件启用 Flash。

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

firefoxProfile = FirefoxProfile()

## Enable Flash

firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
                          'true')

driver = webdriver.Firefox(firefoxProfile)

如果使用 chromedriver 代替 firefox 仍然无法工作,它似乎默认在 chromedriver 中工作。

https://chromedriver.storage.googleapis.com/index.html?path=2.30/

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2014-07-16
    • 1970-01-01
    • 2016-03-28
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多