I am very new to Python programming and I am trying to make a simple application.
What I'm trying to do is search for a text on Google and return the links, my program does this fine. The other thing is if Google has the quick answer like in the photo below, I want to grab it, and this is where my problem lies. I tried searching online and found very few topics in which none of the codes work.
Google Quick box answer:
By examining the code of many pages I noticed that the answer is always in a class called _XWk
but in Python when I get the code of the page and search for this class it doesn't find it. I tried so many ways of scraping the page in Python, but it never gets this class and I think the code it gets is less than the code the browser shows me when I open page source code.
Class _XWk
:
Code:
import requests, lxml
from bs4 import BeautifulSoup
url = 'https://www.google.com/search?q=when%20was%20trump%20born'
h = {"User-Agent":"Chrome/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"}
r = requests.get(url, headers=h).text
soup = BeautifulSoup(r,'lxml')
soup.find_all("div", class_="_xwk")
print (soup)
Any help is appreciated.