Created
June 11, 2021 23:34
-
-
Save fuji44/0c4731717ffe0ea23879c93d8fc145f4 to your computer and use it in GitHub Desktop.
Using request_html in PySimpleGUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import asyncio | |
import PySimpleGUI as sg | |
from requests_html import AsyncHTMLSession | |
import pyppeteer | |
async def print_page_title(window): | |
session = AsyncHTMLSession() | |
browser = await pyppeteer.launch( | |
{ | |
"ignoreHTTPSErrors": True, | |
"headless": True, | |
"handleSIGINT": False, | |
"handleSIGTERM": False, | |
"handleSIGHUP": False, | |
} | |
) | |
session._browser = browser | |
r = await session.get("http://python-requests.org/") | |
await r.html.arender() | |
window.write_event_value("-PrintTitle-", (r.html.find("title", first=True).text)) | |
await session.close() | |
def run(window): | |
new_loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(new_loop) | |
window.write_event_value("-PrintStatus-", ("Processing...")) | |
asyncio.run(print_page_title(window)) | |
window.write_event_value("-PrintStatus-", ("Done!")) | |
new_loop.close() | |
def main(): | |
layout = [ | |
[sg.Text("Status:"), sg.Text(key="-Status-", size=(20, 1))], | |
[sg.Text("Title:"), sg.Text(key="-Title-", size=(20, 1))], | |
[sg.Submit("Print page title")], | |
] | |
window = sg.Window("Window Title", layout, finalize=True) | |
while True: | |
event, values = window.read() | |
if event == sg.WIN_CLOSED: | |
break | |
if event == "Print page title": | |
window["-Title-"].update("") | |
threading.Thread(target=run, args=(window,), daemon=True).start() | |
if event == "-PrintStatus-": | |
window["-Status-"].update(values["-PrintStatus-"]) | |
if event == "-PrintTitle-": | |
window["-Title-"].update(values["-PrintTitle-"]) | |
window.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment