动机(Motivation)
Inspired by this article on SitePoint that shows how to find the Google ranking for a specific page and a search query, I decided to do the same, but for the Yahoo! ranking. The fun part is that my script is a JavaScript and requires nothing but a browser in order to run.
受SitePoint上这篇文章的启发,该文章展示了如何为特定页面和搜索查询找到Google排名,我决定这样做,但对于Yahoo! 排行。 有趣的部分是我的脚本是JavaScript,并且只需要一个浏览器即可运行。
我的与众不同 (How mine is different)
In the article above you need to use Google's SOAP service, so if you're not lucky enough to be running PHP5, you'll probably need something along the lines of PEAR SOAP or NuSOAP. That implies you also need a web server, running PHP. Then you need a Google API key and you need to download stuff and upload it to your server.
在上面的文章中,您需要使用Google的SOAP服务,因此,如果您不足够幸运地无法运行PHP5,则可能需要使用PEAR SOAP或NuSOAP。 这意味着您还需要一个运行PHP的Web服务器。 然后,您需要一个Google API密钥,并且需要下载内容并将其上传到您的服务器。
Nothing even close to that in terms of requirements if you opt in for the Yahoo! web service. All you need is a browser and JavaScript enabled, which shouldn't be a big deal, I don't think 😉
如果您选择使用Yahoo !,就要求而言,甚至没有什么比这更好的了。 网络服务。 您所需要做的只是启用浏览器和JavaScript,这没什么大不了的,我认为don
关于Yahoo! JSON Web服务 (About the Yahoo! JSON web service)
Yahoo's web service can return XML as everybody else, but it can also return serialized PHP and also JSON. Using the JSON option you can make a simple XMLHTTPRequest and get all the content JavaScript-ready, without the headaches of getELementsByTagName() or other DOMmy methods to wrestle that XML tree. The problem here is that you're requesting a file from a different domain, so the browser won't allow it. Workaround - a simple PHP script to serve as a proxy. Oooor (as we said we don't need no stinkin' server) you can use the dynamic JavaScript includes (discussed here) to do the request. As a result you get a working solution with JS only.
Yahoo的Web服务可以像其他所有人一样返回XML,但是它也可以返回序列化PHP和JSON。 使用JSON选项,您可以创建一个简单的XMLHTTPRequest并获取所有可用于JavaScript的内容,而无需费力地处理XML树的getELementsByTagName()或其他DOMmy方法。 这里的问题是您要从其他域请求文件,因此浏览器不允许这样做。 解决方法-一个简单PHP脚本作为代理。 Oooor(正如我们所说,我们不需要任何讨厌的服务器),您可以使用动态JavaScript包含项(在此处讨论)来执行请求。 结果,您只能使用JS获得有效的解决方案。
By the way, if you're wondering about the beauty of JSON, try this eye-opener.
顺便说一句,如果您想了解JSON的美丽之处,请尝试使用这款令人大开眼界的产品。
演示版 (Demo)
Ah, yes, the demo is here.
啊,是的,演示在这里。
Enter a/ your URL, or part of it, and b/ a search query. Then the script will tell you where in the first 1000 results is your URL to be found. If it is found.
输入a /您的URL或部分URL,然后b /搜索查询。 然后,脚本将告诉您在前1000个结果中可以找到您的URL的位置。 如果找到。
怎么运行的 (How it works)
Check the source for the details, it's reasonably well commented, but the big picture:
检查来源以了解详细信息,它的注释相当合理,但总的来说:
You make a request (in
yjsonrank.makeRequest()
) by appending a new SCRIPT element to the HEAD of your HTML. The URL of the script element (the SRC attribute) points to the Y! web service and also passes the search query and a function to be called once the script is included. This function happens to beyjsonrank.process()
通过在HTML的HEAD后面附加一个新的SCRIPT元素来发出请求(在
yjsonrank.makeRequest()
)。 脚本元素的URL(SRC属性)指向Y! Web服务,并传递搜索查询和包含脚本后要调用的函数。 该函数恰好是yjsonrank.process()
The
yjsonrank.process()
function receives JSON data returned by the service, assigned to theresp
variable.yjsonrank.process()
函数接收服务返回的JSON数据,并分配给resp
变量。We loop through
resp.ResultSet
, checking everyresp.ResultSet.Result
if itsUrl
property contains our URL. If yes - we're done! If not, we make another request this time for the next 50 results. (50 is randomly chosen, feel free to modify). We continue until we reach 1000th result, which is the max that Y! will be willing to give.我们遍历
resp.ResultSet
,检查每个resp.ResultSet.Result
的Url
属性是否包含我们的URL。 如果是-我们完成了! 如果没有,我们这次再次请求下一个50个结果。 (随机选择50个,随时进行修改)。 我们继续直到达到第1000个结果,这就是Y的最大值! 愿意付出。
And that's pretty much it, the rest is just fluff and beautifications 😉
就是这样,其余的只是绒毛和美化😉
更多Y! 信息 (More Y! info)
The JSON description
JSON描述
The Web Search API page, listing all the additional parameters you can add to your search request and what format would have the result
Web Search API页面,列出了您可以添加到搜索请求中的所有其他参数以及结果的格式
谢谢阅读!(Thanks for reading!)
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
翻译自: https://www.phpied.com/javascript-to-find-your-yahoo-ranking/