Reputation: 2886
Note: I cannot provide any code as I havent started working of this project yet. I am not looking for code that does the work for me. I wanted suggestions and a direction.
I want to know the best way to access googles search results via python.
Ex: When you type the query Premier League Table
into google search it returns a nice table with all the information:
I only need the information in the table. I googled for answers and came across :
Any suggestions are really helpful
Upvotes: 1
Views: 310
Reputation: 1414
You can use the google-search-results
package to extract the Google sports results.
import os
from serpapi import GoogleSearch
params = {
"engine": "google",
"q": "Premier League Table",
"google_domain": "google.com",
"api_key": os.environ['serpapi_key']
}
search = GoogleSearch(params)
results = search.get_dict()
sports_results = results.get("sports_results", {})
league_standings = sports_results.get("league", {}).get("standings", [])
for league_standing in league_standings:
print(league_standing.get("team", {}).get("name"))
Output
Bournemouth
Arsenal
Aston Villa
Brentford
Brighton
Upvotes: 0
Reputation: 2396
Your best bet is to use selenium (it would be better to use xvfb to avoid having a browser show up, I'm covering the basic case to get you started)
from selenium import webdriver
from lxml import html as lh
url = "http://www.google.com/search?q=premier+league+table"
br = webdriver.Firefox()
br.get(url)
tree = lh.fromstring(br.page_source)
Now you can use xpath expressions to extract elements from the table. For example, this one is a list of 20 teams from that table
tree.xpath('//div[@class="sol-td-entry"]/text()')
Out[36]:
[' Chelsea ',
' Southampton ',
' Man City ',
' Man United ',
' Newcastle ',
' West Ham ',
' Swansea City ',
' Arsenal ',
' Everton ',
' Tottenham ',
' Stoke City ',
' Liverpool ',
' West Brom ',
' Sunderland ',
' Crystal Palace ',
' Hull City ',
' Aston Villa ',
' Leicester City ',
' Burnley FC ',
' QPR ']
Upvotes: 0
Reputation: 102852
Check out the OpenFooty API, as it may have the information you're looking for. Results can be obtained in XML, PHP array, and JSON formats. They seem to have lots of different information available, but not knowing your requirements I can't say if it'll be perfect for you. To be sure, though, it'll be much easier than scraping a bunch of websites.
Good luck!
Upvotes: 1