Search code examples
pythonbeautifulsoupmechanizebing

Using mechanize bing search returns blank page


I am using mechanize to perform a bing search and then I will process the results with beautiful soup. I have successfully performed google and yahoo searches with this same method but when I do a bing search all I get is a blank page.

I am thoroughly confused why this is the case and if anyone can shed any light on the matter that would be greatly appreciated. Here is a sample of the code I'm using:

from BeautifulSoup import BeautifulSoup
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("http://www.bing.com/search?count=100&q=cheese")
content = br.response()
content = content.read()
soup = BeautifulSoup(content, convertEntities=BeautifulSoup.ALL_ENTITIES)
print soup

The result is a blank line printed.


Solution

  • You probably got response that answer is already in your browser cache. Try changing a little you query string, for example decrease count to 50.

    You can also add some debugging code and see headers returned by server:

    br.open("http://www.bing.com/search?count=50&q=cheese")
    response = br.response()
    headers = response.info()
    print headers
    content = response.read()
    

    EDIT:

    I have tried this query with count=100 with Firefox and Opera browsers and it seems that bing do not like such a "big" count. When I decrease count then it works. So this is not mechanize or other Python library fault, but your query is problematic to bing. It also seems that browser can query bing with count=100 but it must first query bing with some smaller count. Strange!