visit
First, let's install the requirements. Save the following into a text file name requirements.txt
requests
bs4
Then run the pip install -r requirements.txt to install the requirements. Then import it into your script.
import urllib
import requests
from bs4 import BeautifulSoup
To perform a search, Google expects the query to be in the parameters of the URL. Additionally, all spaces must be replace with a
+
. To build the URL, we properly format the query and put it into the q parameter.query = "hackernoon How To Scrape Google With Python"
query = query.replace(' ', '+')
URL = f"//google.com/search?q={query}"
Google returns different search results for mobile vs. desktop. So depending on the use case, we need to specify appropriate user-agent.
# desktop user-agent
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
# mobile user-agent
MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36"
headers = {"user-agent" : MOBILE_USER_AGENT}
resp = requests.get(URL, headers=headers)
Now we need to check if the request was successfully. The easiest way is to check the status code. If it returns a
200
, then it was successfully. Then we need to put it into Beautiful Soup to parse the content.if resp.status_code == 200:
soup = BeautifulSoup(resp.content, "html.parser")
results = []
for g in soup.find_all('div', class_='r'):
anchors = g.find_all('a')
if anchors:
link = anchors[0]['href']
title = g.find('h3').text
item = {
"title": title,
"link": link
}
results.append(item)
print(results)