【发布时间】: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>
...
我只想从站点中获取正确的内容/表单,并将 submit 或 post 的数据/表单发送到服务器以模拟 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