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

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

新規登録して質問してみよう
ただいま回答率
85.49%
スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

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

Q&A

解決済

1回答

2023閲覧

OWASP BWAがスクレイピング出来ない

AlbertVerlnich

総合スコア2

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python

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

0グッド

0クリップ

投稿2020/07/05 08:13

前提・実現したいこと

owasp broken web applicationのページ内容を取得したい

発生している問題・エラーメッセージ

TimeoutError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in _new_conn(self)
159 conn = connection.create_connection(
--> 160 (self._dns_host, self.port), self.timeout, **extra_kw
161 )

18 frames
/usr/local/lib/python3.6/dist-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
83 if err is not None:
---> 84 raise err
85

/usr/local/lib/python3.6/dist-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
73 sock.bind(source_address)
---> 74 sock.connect(sa)
75 return sock

TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

NewConnectionError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
676 headers=headers,
--> 677 chunked=chunked,
678 )

/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
391 else:
--> 392 conn.request(method, url, **httplib_request_kw)
393

/usr/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1263 """Send a complete request to the server."""
-> 1264 self._send_request(method, url, body, headers, encode_chunked)
1265

/usr/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1309 body = _encode(body, 'body')
-> 1310 self.endheaders(body, encode_chunked=encode_chunked)
1311

/usr/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1258 raise CannotSendHeader()
-> 1259 self._send_output(message_body, encode_chunked=encode_chunked)
1260

/usr/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
1037 del self._buffer[:]
-> 1038 self.send(msg)
1039

/usr/lib/python3.6/http/client.py in send(self, data)
975 if self.auto_open:
--> 976 self.connect()
977 else:

/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in connect(self)
186 def connect(self):
--> 187 conn = self._new_conn()
188 self._prepare_conn(conn)

/usr/local/lib/python3.6/dist-packages/urllib3/connection.py in _new_conn(self)
171 raise NewConnectionError(
--> 172 self, "Failed to establish a new connection: %s" % e
173 )

NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f8371621208>: Failed to establish a new connection: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

MaxRetryError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
448 retries=self.max_retries,
--> 449 timeout=timeout
450 )

/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
724 retries = retries.increment(
--> 725 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
726 )

/usr/local/lib/python3.6/dist-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
438 if new_retry.is_exhausted():
--> 439 raise MaxRetryError(_pool, url, error or ResponseError(cause))
440

MaxRetryError: HTTPConnectionPool(host='192.168.56.101', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8371621208>: Failed to establish a new connection: [Errno 110] Connection timed out',))

During handling of the above exception, another exception occurred:

ConnectionError Traceback (most recent call last)
<ipython-input-7-8864a8f2ff25> in <module>()
1 session = HTMLSession()
2 session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'})
----> 3 r = session.get("http://192.168.56.101/")

/usr/local/lib/python3.6/dist-packages/requests/sessions.py in get(self, url, **kwargs)
541
542 kwargs.setdefault('allow_redirects', True)
--> 543 return self.request('GET', url, **kwargs)
544
545 def options(self, url, **kwargs):

/usr/local/lib/python3.6/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp

/usr/local/lib/python3.6/dist-packages/requests/sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)

/usr/local/lib/python3.6/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
514 raise SSLError(e, request=request)
515
--> 516 raise ConnectionError(e, request=request)
517
518 except ClosedPoolError as e:

ConnectionError: HTTPConnectionPool(host='192.168.56.101', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8371621208>: Failed to establish a new connection: [Errno 110] Connection timed out',))

該当のソースコード

python

1from requests import Request, Session 2 3session = Session() 4obj_request = Request("GET", "http://192.168.56.101/") 5obj_prepped = session.prepare_request(obj_request) 6obj_response = session.send(obj_prepped, 7 verify=True, 8 timeout=60 9 )

試したこと

・Sessionの代わりにrequest-htmlのHTMLSessionを使いました
・送信するリクエストのヘッダを変更しました
session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'})

以上の変更では結果は変わらなかったです。

・以下のコードでseleniumのchromedriverを試してみました
from selenium import webdriver
from bs4 import BeautifulSoup

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=options)
driver.get("http://192.168.56.101/")
print(driver.current_url) #出力は「http://192.168.56.101/」
soup = BeautifulSoup(driver.page_source)

エラーは出ませんでしたが、soupの中身は「<html><head></head><body></body></html>」のみとなっていました。

ちなみに自分のブラウザから直接OWASPにアクセスすると問題なくページが表示されます。

補足情報(FW/ツールのバージョンなど)

実行環境
環境:google colaboratory
Python:Python 3.6.9
OS:Windows10
プロキシ:有効にしていない

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

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

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

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

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

Penpen7

2020/07/06 23:20

google colaboratoryを使われている理由はなんですか?
AlbertVerlnich

2020/07/08 06:18

元々使っていたPythonの実行環境が重くて動かなくなった為、一から環境を構築する手間が省けるGoogle Colaboratoryを使っています。 やはりGoogle Colaboratoryがまずいのでしょうか?
Penpen7

2020/07/08 08:11

Google ColaboratoryというのはGoogleが提供する外部のサービスですよね? そこにプライベートIPである192.168.56.101を突っ込んで接続しようとしても無理な話だと思うんですが... ご自分のPCからなら繋がるというのも頷けます。
Penpen7

2020/07/08 08:14 編集

やるならパブリックIPを入れないと繋がらないと思います。 もしくはローカルのpythonを使うかのどちらかでしょう。
AlbertVerlnich

2020/07/10 04:26

何度も申し訳ないですが、パブリックIPをどのように入れれば良いのでしょうか? "http://(パブリックIP)/"と入力しても接続できなかったのですが……
Penpen7

2020/07/11 22:40

ポート開放しないと繋げられないです。ただし、セキュリティの問題が出てくるのため、対策はちゃんとやっておかないといけません。 python環境が重くて使えないほどのスペックであれば、買い換えた方がいいと思います。
guest

回答1

0

ベストアンサー

http://192.168.56.101/ は私有IPアドレスですので、ご自身のパソコンで OWASP BWA をインストールしたのを推測します。残念ですが、Google Colab からそのプライベートIPアドレスと通信できません。

OWASP BWA は不安全なアプリであり、パブリックIPアドレスでインターネットに晒してはいけないのです。ご自身のパソコンで Google Colab に似たような環境を作らなければなりません。

そのためなら、Anaconda がおすすめです。インストールしたら、メイン画面に Jupyter Notebook を開けて、右上の New を押したら、Python のノートブックを選べます。そしてノートブックの中で問題文のコードを実行して見てください。

投稿2020/07/15 04:23

編集2020/07/15 04:25
YufanLou

総合スコア463

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

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

Penpen7

2020/07/15 07:10

環境構築はできているようですが、スペック不足のようです。
YufanLou

2020/07/15 10:01

そうですか!それは残念です。
AlbertVerlnich

2020/07/18 14:03

jupyter notebookで同じコードを試してみて、無事スクレイピング出来ました。 Penpen7さん、YufanLouさんアドバイスありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問