From 500761e16e6d9082802d124ef14fd8045607e7ad Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Mon, 28 Oct 2024 17:36:45 -0500 Subject: [PATCH] Fix %help web request --- nbs/09_magics.ipynb | 4 +++- nbstata/magics.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/nbs/09_magics.ipynb b/nbs/09_magics.ipynb index f85a6dc..eaee692 100644 --- a/nbs/09_magics.ipynb +++ b/nbs/09_magics.ipynb @@ -1027,7 +1027,9 @@ " html_base = \"https://www.stata.com\"\n", " html_help = urllib.parse.urljoin(html_base, \"help.cgi?{}\")\n", " url_safe_code = urllib.parse.quote(code)\n", - " reply = urllib.request.urlopen(html_help.format(url_safe_code))\n", + " headers = {\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\"}\n", + " request = urllib.request.Request(html_help.format(url_safe_code), headers=headers)\n", + " reply = urllib.request.urlopen(request)\n", " html = reply.read().decode(\"utf-8\")\n", "\n", " # Remove excessive extra lines (Note css: \"white-space: pre-wrap\")\n", diff --git a/nbstata/magics.py b/nbstata/magics.py index 62b880c..cf74e77 100644 --- a/nbstata/magics.py +++ b/nbstata/magics.py @@ -362,7 +362,9 @@ def _get_help_html(self, code): html_base = "https://www.stata.com" html_help = urllib.parse.urljoin(html_base, "help.cgi?{}") url_safe_code = urllib.parse.quote(code) - reply = urllib.request.urlopen(html_help.format(url_safe_code)) + headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"} + request = urllib.request.Request(html_help.format(url_safe_code), headers=headers) + reply = urllib.request.urlopen(request) html = reply.read().decode("utf-8") # Remove excessive extra lines (Note css: "white-space: pre-wrap")