If you haven't solved this problem yet, it looks like the reason BeautifulSoup doesn't find anything because resultStats never appears in the soup is because your query (page_google) returns only JavaScript and not the search results that JavaScript dynamically loads. You can verify this by adding
print(soup)
for your code and you will see that the resultStats div is not showing.
The following code:
import sys from urllib2 import Request, urlopen import urllib from bs4 import BeautifulSoup query = 'pokerbonus' url = "http://www.google.de/search?q=%s" % urllib.quote_plus(query) req_google = Request(url) req_google.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3') html_google = urlopen(req_google).read() soup = BeautifulSoup(html_google) scounttext = soup.find('div', id='resultStats') print(scounttext)
Will be printed
<div class="sd" id="resultStats">Ungefรคhr 1.060.000 Ergebnisse</div>
Finally, using a tool like Selenium Webdriver may be the best way to resolve this issue, since Google does not allow bots to scratch the search results.
source share