What is an appropriate way to datamine the total number of results of a keyword search?
newbie programmer and lurker here, hoping for some sensible advice. :)
Using a combination of Python, BeautifulSoup, and the Bing API, I was able to find what I wanted with the following code:
import urllib2
from BeautifulSoup import BeautifulStoneSoup
Appid = #My Appid
query = #My query
soup = BeautifulStoneSoup(urll开发者_Python百科ib2.urlopen("http://api.search.live.net/xml.aspx?Appid=" + Appid + "&query=" + query + "&sources=web"))
totalResults = soup.find('web:total').text
So I'd like to do this across a few thousand search terms and was wondering if
- doing this request a thousand times would be construed as hammering the server,
- what steps I should take to not hammer said servers (what are best practices?), and
- is there a cheaper (data) way to do this using any of the major search engine APIs?
It just seems unnecessarily expensive to grab all that data just to grab one number per keyword and I was wondering if I missed anything.
FWIW, I did some homework and tried the Google Search API (deprecated) and Yahoo's BOSS API (soon to be deprecated and replaced with a paid service) before settling with the Bing API. I understand direct scraping of a page is considered poor form so I'll pass on scraping search engines directly.
There are three approaches I can think of that have helped previously when I had to do large scale URL resolution.
- HTTP Pipelining (another snippet here)
- Rate-limiting server requests per IP (i.e., each IP can only issue 3 requests / second). Some suggestions can be found here: How to limit rate of requests to web services in Python?
- Issuing requests through an internal proxy service, using
http_proxy
to redirect all requests to said service. This proxy service will then iterate over a set of network interfaces and issue rate limited requests. You can use Twisted for that.
With regard to your question 1, Bing has an API Basics PDF file that summarizes the terms and conditions in human-readable form. In the "What you must do" section. That includes the following statement:
Restrict your usage to less than 7 queries per second (QPS) per IP address. You may be permitted to exceed this limit under some conditions, but this must be approved through discussion with api_tou@microsoft.com.
If this is just a one-off script, you don't need to do anything more complex than just adding a sleep
between making requests, so that you're making only a couple of requests a second. If the situation is more complex, e.g. these requests are being made as part of a web service, the suggestions in Mahmoud Abdelkader's answer should help you.
精彩评论