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

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

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

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

Q&A

1回答

1195閲覧

ウェブクローリングのプログラムが実行できません。File "<stdin>", line 1, in <module>

fdaf_11111

総合スコア16

Python

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

0グッド

0クリップ

投稿2017/08/27 09:04

ウェブクローリングのプログラムが実行できません。

File "<stdin>", line 1, in <module> NameError: name 'spider' is not defined ```というエラーが出ます。 ウェブクローリングのプログラムは以下です。 ```Python from html.parser import HTMLParser from urllib.request import urlopen from urllib import parse class LinkParser (HTMLParser): def handle_starttag (self, tag, attrs): if tag == 'a': for (key, value) in attrs: if key == "href": newUrl = parse.urljoin (self.baseUrl, value) self. links = self. links + [newUrl] def getLinks (self, url) : self.links = [ ] self.baselUrl = url response = urlopen(url) if response. getheader ("Content-Type") =="text/html": htmlBytes = response.read() htmlString = htmlBytes.decode("utf-8") self.feed(htmlString) return htmlString, self. links else: return "", [ ] def spider (url, word, maxPages): pagesToVisit = [url] numberVisited = 0 foundWord = False while numberVisited < maxPages and pagesToVisit != [ ] and not foundWord: numberVisited = numberVisited +1 url = pagesToVisit [0] pagesToVisit = pagesToVisit [1: ] try: print (numberVisited, "Visiting: ", url) parser = LinkParser () data, links = parser.getLinks (url) if data.find (word) >-1 : foundWord = True pagesToVisit = pagesToVisit + links print(" **success!**") except: print(" **Faiied!**") if foundWord: print("The word", word, "was found at", url) else: print ("Word never found")

というコードを書きました。これをターミナルで実行しようとすると

$ python Python 3.5.0 (default, Aug 27 2017, 01:31:27) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> spider("https://www.yahoo.co.jp/","yahoo",1) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'spider' is not defined >>>

というエラーが出てしまいます。
解決方法がわからないので、よろしくお願いします。

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

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

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

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

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

guest

回答1

0

$ python Python 3.5.0 (default, Aug 27 2017, 01:31:27) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> spider("https://www.yahoo.co.jp/","yahoo",1) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'spider' is not defined >>>

spiderという関数をimportしていないために、定義されていないというエラーがでているのでは?

投稿2017/08/27 09:30

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問