質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

638閲覧

pythonのブラウザの起動について

gef57378

総合スコア7

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/10/04 01:27

これは「ロメオとジュリエット」の戯曲全文を掲載したテキストページの冒頭250文字だけを表示しています。
(178978文字があり、最初の250文字だけ表示)

import requests res=requests.get("https://automatetheboringstuff.com/files/rj.txt") type(res) ↓↓↓ requests.models.Response res.status_code==requests.codes.ok ↓↓↓ True len(res.text) ↓↓↓ 178978 print(res.text[:250]) ↓↓↓ The Project Gutenberg EBook of Romeo and Juliet, by William Shakespeare This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Projec   #冒頭250文字が表示

これにそのロミオとジュリエットの全文が書かれているWebブラウザ("https://automatetheboringstuff.com/files/rj.txt")を起動できるようにしたいのですが、以下のコードでwebbrowser.open("https://automatetheboringstuff.com/files/rj.txt")を追加してもエラーが出てしまいます。
どうすればWebブラウザが立ち上げられるようにすれば良いか教えてほしいです。
よろしくお願いします。

import requests res=requests.get("https://automatetheboringstuff.com/files/rj.txt") type(res) ↓↓↓ requests.models.Response res.status_code==requests.codes.ok ↓↓↓ True len(res.text) ↓↓↓ 178978 print(res.text[:250]) ↓↓↓ The Project Gutenberg EBook of Romeo and Juliet, by William Shakespeare This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Projec webbrowser.open("https://automatetheboringstuff.com/files/rj.txt") ↓↓↓ NameError Traceback (most recent call last) <ipython-input-27-e491f8d55c28> in <module> ----> 1 webbrowser.open("https://automatetheboringstuff.com/files/rj.txt") NameError: name 'webbrowser' is not defined #エラーが表示

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

import webbrowserを先に書いてください。

Python

1import requests 2import webbrowser 3 4res = requests.get("https://automatetheboringstuff.com/files/rj.txt") 5 6print(type(res)) 7print(res.status_code == requests.codes.ok) 8print(len(res.text)) 9print(res.text[:250]) 10 11webbrowser.open("https://automatetheboringstuff.com/files/rj.txt")

投稿2020/10/04 01:54

Daregada

総合スコア11990

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gef57378

2020/10/04 02:59

ありがとうございます! 完全に忘れてました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問