Skip to content

First Script for Web Scraping#

When you have installed relevant packages, you're ready to go. Simply type:

Python
1
2
3
4
5
6
7
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    headline = browser.get.text("//h1")
    print(headline)
    browser.wait.seconds(5)

Alternatively, if you would prefer not to use the built-in context manager that automatically closes the browser when it has finished or an error occurs, you can manually close the browser using the browser.quit() method:

Python
1
2
3
4
5
6
7
8
from browserist import Browser

browser = Browser()
browser.open.url("https://example.com")
headline = browser.get.text("//h1")
print(headline)
browser.wait.seconds(5)
browser.quit()

How to Use Different Browsers#

If you have already installed any of the recommended browser drivers for Chrome, Edge, or Firefox browser drivers, you can try them with Browserist. Simply select the desired browser type in the BrowserSettings configuration:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from browserist import Browser, BrowserSettings, BrowserType

chrome = BrowserSettings(type=BrowserType.CHROME)
edge = BrowserSettings(type=BrowserType.EDGE)
firefox = BrowserSettings(type=BrowserType.FIREFOX)

for settings in [chrome, edge, firefox]:
    with Browser(settings) as browser:
        browser.open.url("https://example.com")
        browser.wait.seconds(5)

Learn more about the recommended browser drivers for Browserist.