TJ1
TJ1

Reputation: 8478

Python: extract number of indexed pages in Google

If we type a search term in Google search box, it returns something like:

About x results (y seconds)

How can I extract this line from the search results using Python 3? Thanks for your help.

Upvotes: 0

Views: 317

Answers (1)

falsetru
falsetru

Reputation: 369064

Using lxml (and cssselect):

>>> import urllib.request
>>> import lxml.html
>>> html = .. urllib.request.urlopen .. http://www.google.com/search?q=python ..
           .read()
>>> root = lxml.html.fromstring(html)
>>> div, = root.cssselect('#resultStats')
>>> div.text_content().strip()
'About 46,600,000 results  (0.17 seconds)'

Upvotes: 3

Related Questions