最新消息:

设置PhantomJS的User-Agent

phantomjs admin 1395浏览 0评论

有些网站的WebServer对User-Agent有限制,可能会拒绝不熟悉的User-Agent的访问,所以,写Web自动化代码可能需要将 User-Agent稍微伪装一下,否则可能会被拒绝访问。这里简单记录一下Selenium中使用PhantomJS,设置User-Agent的方 法。

默认情况下,是没有自动设置User-Agent的;设置PhantomJS的user-agent,是要设置“phantomjs.page.settings.userAgent”这个desired_capability.
Python代码如下:

'''
Created on Dec 6, 2013
@author: Jay
@summary: Set user-agent before using PhantomJS to get a web page.
'''
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 "
)
driver = webdriver.PhantomJS(executable_path='./phantomjs', desired_capabilities=dcap)
driver.get("http://dianping.com/")
cap_dict = driver.desired_capabilities
for key in cap_dict:
	print '%s: %s' % (key, cap_dict[key])
print driver.current_url
driver.quit

转自:http://smilejay.com/2013/12/set-user-agent-for-phantomjs/

转载请注明:jinglingshu的博客 » 设置PhantomJS的User-Agent


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /usr/share/nginx/html/wp-content/themes/d8/comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址