Jalil
Jalil

Reputation: 101

Suggestions: Any module that can work as a browser to execute web pages in python?

I used requests library to get an Html content, in my tkinter project

I tried to save the content in an html file and open it using my browser, and it opened normally just like in the original website.

Now, I don't wont to use my browser to open this Html content, because my laptop is not that powerful and opening the browser takes a lot of time (Means that I tried 'webbrowser' module, and I didn't like it), so I looked for a simpler Html runner and I have found 'tkinterhtml', it's easy to use and fast but the problem is that it didn't show me the result like it is in the website and like I have seen when I have opened the html file using a web browser, because the result was without any colors and it was just black text in white backgroud (So actually this is not what I'm looking for).

Any suggestions dear programmers?

Upvotes: 1

Views: 560

Answers (4)

Jalil
Jalil

Reputation: 101

I used PyQt5 and it worked good and pretty fast for me:

First: Install the PyQt5 and PyQtWebEngine packages:

pip3 install PyQt5
pip3 install PyQtWebEngine

Than:I created a class of a simple browser

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
    def __init__(self, url):
        super(MainWindow, self).__init__()
        self.setWindowTitle("Title")
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl(url))
        self.setCentralWidget(self.browser)
        self.resize(683, 1000)
        self.move(683, 0)

Finally: When I want to run the browser:

app = QApplication(sys.argv)
window = MainWindow('https://www.google.com/')
window.show()
app.exec_()

PS: I'm using Kali linux as a root user and Chrome doesn't run with me without the argument --no-sendbox, so if you have the same situation, you need to append the argument to the sys.argv before the line app = QApplication(sys.argv) using this simple line of code:

sys.argv.append("--no-sandbox")

Upvotes: 1

Bram Teunis
Bram Teunis

Reputation: 1

You can make it by yourself using this:

import webbrowser
from tkinter import *
tk=Tk();tk.geometry("500x500")
url = StringVar()

Entry(tk,textvariable=url).place(x=0,y=0)
def urllink():
    webbrowser.open_new_tab(f'{url}')
Button(tk,text="submit",command=urllink).place(x=0,y=50)

Upvotes: 0

thomasXu
thomasXu

Reputation: 161

Try Splash Installation — Splash 3.3.1 documentation

Splash - A javascript rendering service

Splash is a javascript rendering service. It’s a lightweight web browser with an HTTP API, implemented in Python 3 using Twisted and QT5. The (twisted) QT reactor is used to make the service fully asynchronous allowing to take advantage of webkit concurrency via QT main loop. Some of Splash features:

  • process multiple webpages in parallel;
  • get HTML results and/or take screenshots;
  • turn OFF images or use Adblock Plus rules to make rendering faster;
  • execute custom JavaScript in page context;
  • write Lua browsing scripts;
  • develop Splash Lua scripts in Splash-Jupyter Notebooks.
  • get detailed rendering info in HAR format.

Upvotes: 0

zeit
zeit

Reputation: 377

You must be looking for Selenium.


Here is a simple guide for you.

Before coding:

  1. pip install selenium

  2. download webdriver (say you have had Chrome installed in your computer, then you should download ChromeDriver accordingly)

    • make sure the version of ChromeDriver is matched with Chrome(see version selection)
    • remember to put the chromedriver.exe under your python \Scripts folder

Now do the cool thing with codes:

from selenium import webdrive

browser = webdrive.Chrome()
browser.get('https://www.example.com/')

Upvotes: 1

Related Questions