0

If I google How old is Messi, it should give me output: 30 but it instead answers me None.
I'm using Python 3.

import time
from bs4 import BeautifulSoup
import requests
search=input("What do you want to ask: ")
search=search.replace(" ","+")
link="https://www.google.com/search?q="+search
print(link)
source=requests.get(link).text

soup=BeautifulSoup(source,"html.parser")
print(soup.prettify())
answer=soup.find('div',class_="Z0LcW")
print(answer.text)

2 Answers2

2

You have to add User Agent to fake a real browser visit:

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
source=requests.get(link, headers=headers).text
soup=BeautifulSoup(source,"html.parser")

Just tried that, it works. See this answer for more info.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
-3

First of all your soup.find is wrong, you have to make it like this
answer=soup.find('div',{'class':'Z0LcW'})
Most likely the issue is you need something to load JS data. You can use selenium + ChromeDriver

import selenium
from selenium import webdriver
driver = webdriver.Chrome()
import time
from bs4 import BeautifulSoup
import requests

search=input("What do you want to ask: ")
search=search.replace(" ","+")
link="https://www.google.com/search?q="+search
driver.get(link)
time.sleep(2)
driver.implicitly_wait(5)
html = driver.page_source
soup=BeautifulSoup(html, "html.parser")
print(soup.prettify())
answer=soup.find('div',{'class':"Z0LcW")
print(answer.text)

Hopefully that'll help. P.S: Would suggest following PEP-8 style guide

nexla
  • 434
  • 7
  • 20