Python Forum
How to use BeautifulSoup to parse google search results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use BeautifulSoup to parse google search results
#12
(Dec-21-2017, 09:10 PM)DevinGP Wrote:
(Dec-21-2017, 07:33 PM)metulburr Wrote: Then it probably is using javscript and you are only left with selenium as an option.

I didnt know the results might be javascript though.

Do you mind telling me how I would implement Selenium into my current code or at least pointing me to a tutorial on someone using it to scrape the titles and summaries? Thank you!

from selenium import webdriver
import time
from bs4 import BeautifulSoup

DRIVERPATH = '/home/metulburr/chromedriver' 

class Data:
    def __init__(self, search):
        self.url = 'https://www.google.com/'
        self.setup_driver(self.url)
        #self.browser.delete_all_cookies()
        self.search = search
        self.handle_search()
        self.get_data()
        time.sleep(1010000000)
        
    def get_data(self):
        soup = BeautifulSoup(self.browser.page_source, 'html.parser')
        divs = soup.find_all('div', {'class':'g'})
        for div in divs:
            print(div.a.text)
            print(div.a['href'])
            desc = div.find('span', {'class':'st'})
            print(desc.text)
        
        
    def handle_search(self):
        self.browser.find_element_by_xpath('//*[@id="lst-ib"]').click()
        self.browser.find_element_by_id("lst-ib").send_keys(self.search)
        time.sleep(1)
        self.browser.find_element_by_xpath('//*[@id="sbtc"]/div[2]/div[2]/div[1]/div/ul/li[7]/div/span[1]/span/input').click()
        time.sleep(1)
        
    def setup_driver(self, url):
        self.browser = webdriver.Chrome(DRIVERPATH)
        self.browser.set_window_position(0,0)
        self.browser.get(self.url)

data = Data('python forum')
data.browser.quit()
Recommended Tutorials:
Reply


Messages In This Thread
RE: How to use BeautifulSoup to parse google search results - by metulburr - Dec-22-2017, 12:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to convert browser generated xml to parse in BeautifulSoup Nik1811 0 365 Mar-22-2024, 01:37 PM
Last Post: Nik1811
  Using BeautifulSoup And Getting -1 Results knight2000 10 3,118 Mar-07-2023, 02:42 PM
Last Post: snippsat
  Web scraping for search results JOE 7 3,408 May-14-2022, 01:19 PM
Last Post: JOE
  With Selenium create a google Search list in Incognito mode withe specific location, tsurubaso 3 3,329 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  Wrong number of google results in a date range Val 0 1,893 Mar-15-2020, 02:29 PM
Last Post: Val
  Project: “I’m Feeling Lucky” Google Search Truman 31 28,843 Jul-09-2019, 04:20 PM
Last Post: tab_lo_lo
  Outputing the results of search machine Emmanouil 4 5,127 Nov-07-2016, 05:20 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020