本文介绍了如何在Rails中为Mechanize设置自定义用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道您有一组预定义的别名,例如,可以通过设置agent.user_agent_alias ='Linux Mozilla'来使用,但是如果我要设置自己的用户代理,那在写Web爬网程序时该怎么办?并希望识别它,以便我索引的网站.就像Googlebot.

I know you have a set of pre-defined aliases you can use by setting agent.user_agent_alias = 'Linux Mozilla' for instance, but what if I want to set my own user agent, as I'm writing a web crawler and want to identify it, for the sites I'm indexing's sake. Just like Googlebot.

似乎有一个user_agent方法,但我似乎找不到任何有关其功能的文档.

There seems to be a user_agent method, but I can't seem to find any documentation about it's function.

推荐答案

您可以通过别名设置用户代理

You can set the user agent from an alias

a = Mechanize.new
a.user_agent_alias = 'Mac Safari'

可用别名存储在AGENT_ALIASES常量中.

Available aliases are stored in the AGENT_ALIASES constant.

p Mechanize::AGENT_ALIASES

否则,请使用#user_agent设置您的自定义用户代理.

Otherwise, use #user_agent to set your custom user agent.

a = Mechanize.new
a.user_agent = 'Custom agent'

这篇关于如何在Rails中为Mechanize设置自定义用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:52