Utkarsh Agrawal
Utkarsh Agrawal

Reputation: 65

Python search engine error while accessing the code

Here is the code I am using.

from google import google

search_results = google.search("this is test", 3)
for result in search_results:
    print(result.description)

Now when I was trying to access it I was getting this error

 Error accessing: http://www.google.com/search?q=

What will be the problem?

EDIT: I am not asking the alternative, I just ask Why this problem occurs? What will be the solution?

Upvotes: 0

Views: 124

Answers (1)

Himanshu dua
Himanshu dua

Reputation: 2513

You can use requests python library to search on google. Install requests by pip install requests You can use google to search anything and parse the result with Beautifulsoup. Following code search the query on google and after that BeautifulSoup is used to get the URLs that Google returned.

import requests
import urllib
from bs4 import BeautifulSoup

query = 'any search term'


r = requests.get('https://www.google.com/search?q={}'.format(query))
soup = BeautifulSoup(r.text, "html.parser")
links = []
for item in soup.find_all('h3', attrs={'class' : 'r'}):
    links.append(item.a['href'])

print(links)

Upvotes: 1

Related Questions